在UITableViewCell上设置UIImage尺寸图像

问题描述:

我有一个标准的UITableViewCell,我使用文本和图像属性来显示favicon.ico和一个标签。由于UIImage支持ICO格式,所以在大部分情况下,这种方法非常有效。但是,有些网站(如Amazon.com说)具有favicon.ico,它们利用ICO格式在同一文件中存储多个尺寸的能力。亚马逊存储四种不同的尺寸,一直到48x48。在UITableViewCell上设置UIImage尺寸图像

这导致大多数图像为16x16,除了少数以32x32或48x48进来的图像,并且使所有内容看起来都很糟糕。我在这里搜索,官方论坛,文档和其他地方没有成功。我已经尝试了所有我能想到的限制图像大小的方法。唯一有效的方法是无证的方法,我不打算使用。这是我的第一个应用程序,也是我第一次使用Cocoa(来自C#)。

万一我不是在什么我要找的明确,理想情况下,建议将围绕着设置UIImage的尺寸,以便48x48的版本将向下扩展到16×16或方法来告诉UIImage使用ICO文件中存在16x16版本。我不一定需要代码:只是一个方法的建议会让我很好。

有没有人有任何建议? (我在官方论坛as well上问过,因为我已经沉浸了一天以上,如果有解决方案发布在那里,我也会把它放在这里。)

下面是用户在官方论坛posted是最终解决问题(与自己有轻微的修改,使其静态纳入到一套实用方法)“BCD”过:

+ (UIImage *)scale:(UIImage *)image toSize:(CGSize)size 
{ 
    UIGraphicsBeginImageContext(size); 
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return scaledImage; 
} 

我不知道任何关于如何ico文件工作,所以也许可以轻松地提取16x16图像并使用它,但我不知道如何。

据我所知,无法在通用的UITableViewCell的图像上设置任何有用的属性。最简单的方法(特别是如果你是新手)将所有东西缩小到16x16,将是子类UITableViewCell,给它一个UIImageView,总是将图像视图的大小设置为16x16,并将其contentMode设置为UIViewContentModeAspectFit

+0

我试过这种方法,但没有奏效,但我可能只是做错了。 – bbrown 2009-06-02 21:19:06

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

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; 
} 

然后创建一个约束比例的新图像。用从亚马逊获得的ICO文件替换“图片”。

CGRect sz = CGRectMake(0.0f, 0.0f, 16, 16); 
UIImage *resized = resizedImage(image, sz); 
+0

出于某种原因,您的代码为我抛出了很多CoreGraphics错误。这可能是ICO格式的问题。虽然这是一个很好的答案。 – bbrown 2009-06-02 21:16:46

+1

他们编译/链接错误?我不认为Xcode默认链接到CoreGraphics框架的新项目,所以你需要添加它和相应的@import语句。 – 2009-06-02 21:23:54

+1

是的,你需要链接到CoreGraphics框架。如果你不这样做,那就是你得到这些错误的原因。 – 2009-06-02 23:45:57

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

虽然关于每行字节数设置不够高 - 我在阅读这个post的答案后最终将其设置为0,这让系统确定了正确的值,但我在控制台上得到了相当数量的警告。

如:

CGContextRef bitmap = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, CGColorSpaceCreateDeviceRGB(), alphaInfo); 

这里是我做到了。该技术采用适当移动文本和细节文本标签的左边的护理:

@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; 
} 

我已经修改别人的人的类别,以使所有的图像占据相同的宽度(可视宽度),无需缩放:

-(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; 
}