代码之家  ›  专栏  ›  技术社区  ›  bbrown

在uiTableViewCell图像上设置uiImage维度

  •  7
  • bbrown  · 技术社区  · 16 年前

    我有一个标准 UITableViewCell 使用文本和图像属性显示 favicon.ico 还有一个标签。在很大程度上,这是非常有效的,因为 UIImage 支持ICO格式。但是,有些网站(如 Amazon.com 说) 图标文件 利用ICO格式在同一文件中存储多个大小的功能。亚马逊有四种不同的尺寸,一路高达48x48。

    这导致大多数图像是16x16,除了一些32x32或48x48的图像,使一切看起来很糟糕。我在这里、官方论坛、文档以及其他地方搜索都没有成功。我已经尝试了所有我能想到的方法来限制图像的大小。唯一起作用的是一种未记录的方法,我不打算使用它。这是我的第一个应用程序,也是我对可可的第一次体验(来自C)。

    如果我不清楚我在寻找什么,理想情况下建议将围绕设置 紫外图像 这样48x48版本就可以缩小到16x16或一种方法 紫外图像 使用ICO文件中的16x16版本。我不一定需要代码:仅仅建议一个方法就可以了。

    有人有什么建议吗?(我在官方论坛上问过 as well 因为我已经沉迷其中一天多了。如果有解决方案贴在那里,我也会把它贴在这里。)

    6 回复  |  直到 12 年前
        1
  •  23
  •   bbrown    16 年前

    以下是官方论坛上的“BCD”用户 posted 最终解决了这个问题(我自己稍微修改一下,使其成为静态的,以便包含在一组实用方法中):

    + (UIImage *)scale:(UIImage *)image toSize:(CGSize)size
    {
        UIGraphicsBeginImageContext(size);
        [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
        UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return scaledImage;
    }
    
        2
  •  4
  •   Brandon Schlenker    16 年前

    我在我的应用程序中做了类似的事情。

        UIImage* resizedImage(UIImage *inImage, CGRect thumbRect)
    {
        CGImageRef          imageRef = [inImage CGImage];
        CGImageAlphaInfo    alphaInfo = CGImageGetAlphaInfo(imageRef);
    
        if (alphaInfo == kCGImageAlphaNone)
            alphaInfo = kCGImageAlphaNoneSkipLast;
    
        CGContextRef bitmap = CGBitmapContextCreate (NULL, thumbRect.size.width, thumbRect.size.height, 8, thumbRect.size.width*3, 
                                                                                     CGColorSpaceCreateDeviceRGB(), alphaInfo);
    
        CGContextDrawImage(bitmap, thumbRect, imageRef);
    
        CGImageRef  ref = CGBitmapContextCreateImage(bitmap);
        UIImage*    result = [UIImage imageWithCGImage:ref];
    
        CGContextRelease(bitmap);
        CGImageRelease(ref);
    
        return result;
    }
    

    然后创建一个约束比例的新图像。将“image”替换为从Amazon获得的ICO文件。

    CGRect sz = CGRectMake(0.0f, 0.0f, 16, 16);
    UIImage *resized = resizedImage(image, sz);
    
        3
  •  1
  •   Community CDub    8 年前

    还没有足够的业力来评论答案,但是我在上面的bbrandons代码方面取得了很好的成功。

    我确实在控制台上得到了相当数量的警告,尽管在阅读了这个问题的答案之后,每行字节数设置还不够高。 post 我最终将其设置为0,这样系统就可以确定正确的值。

    如:

    CGContextRef bitmap = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, CGColorSpaceCreateDeviceRGB(), alphaInfo);
    
        4
  •  1
  •   Chris    14 年前

    我就是这样做的。此技术负责将文本和细节文本标签适当向左移动:

    @interface SizableImageCell : UITableViewCell {}
    @end
    @implementation SizableImageCell
    - (void)layoutSubviews {
        [super layoutSubviews];
    
        float desiredWidth = 80;
        float w=self.imageView.frame.size.width;
        if (w>desiredWidth) {
            float widthSub = w - desiredWidth;
            self.imageView.frame = CGRectMake(self.imageView.frame.origin.x,self.imageView.frame.origin.y,desiredWidth,self.imageView.frame.size.height);
            self.textLabel.frame = CGRectMake(self.textLabel.frame.origin.x-widthSub,self.textLabel.frame.origin.y,self.textLabel.frame.size.width+widthSub,self.textLabel.frame.size.height);
            self.detailTextLabel.frame = CGRectMake(self.detailTextLabel.frame.origin.x-widthSub,self.detailTextLabel.frame.origin.y,self.detailTextLabel.frame.size.width+widthSub,self.detailTextLabel.frame.size.height);
            self.imageView.contentMode = UIViewContentModeScaleAspectFit;
        }
    }
    @end
    
    ...
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[SizableImageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    
        cell.textLabel.text = ...
        cell.detailTextLabel.text = ...
        cell.imageView.image = ...
        return cell;
    }
    
        5
  •  0
  •   zem    16 年前

    我对ICO文件的工作原理一无所知,所以也许可以轻松地提取16x16图像并使用它,但我不知道如何操作。

    据我所知,不可能在普通图像中设置任何有用的属性 UITableViewCell . 将所有内容缩小到16x16的最简单方法(仍然是混乱的,特别是如果您是新手)是子类 UITababVIEW单元格 给它一个 UIImageView ,始终将图像视图的大小设置为16x16,并将其ContentMode设置为 UIViewContentModeAspectFit .

        6
  •  0
  •   LolaRun    13 年前

    我已经修改了其他人的人物类别,以使所有图像占用相同的宽度(可见宽度),而不缩放:

    -(UIImage*) centerImage:(UIImage *)inImage inRect:(CGRect) thumbRect
    {
    
        CGSize size= thumbRect.size;
        UIGraphicsBeginImageContext(size);  
        //calculation
        [inImage drawInRect:CGRectMake((size.width-inImage.size.width)/2, (size.height-inImage.size.height)/2, inImage.size.width, inImage.size.height)];
        UIImage *newThumbnail = UIGraphicsGetImageFromCurrentImageContext();        
        // pop the context
        UIGraphicsEndImageContext();
        return newThumbnail;
    }