我有其中我使用的文字和图像的属性来显示UITableViewCell和标签标准favicon.ico。在大多数情况下,这个作品真的很好,因为UIImage支持ICO格式。然而,一些网站(如 Amazon.com 说)有favicon.icos,使使用的ICO格式的能力保存在同一文件的多个尺寸。亚马逊存储四个不同的尺寸,所有一直到48×48。

这导致大多数图像是除少数,在32×32或48×48进来,让一切看起来可怕的16×16。我已经在这里搜索,官方论坛,文档,并没有成功别处。我曾经试过,我能想到的约束图像尺寸的一切。只有工作的事情是一个未公开的方法,这我不会使用。这是我的第一个应用程序,我用可可第一次经历(来自C#)。

在情况下,我是不是在我要找清晰,最好的建议是围绕着设置UIImage的尺寸,以便48x48的版本将向下扩展到16×16或方法来告诉UIImage使用16×16目前在ICO文件版本。我不一定需要代码:只是一个方法的建议,会做我的罚款。

有没有人有什么建议? (我问在官方论坛以及的,因为我已经沉没超过一天到此了。如果一个解决方案张贴在那里,我把它在这里。)

有帮助吗?

解决方案

下面就是用户在官方论坛“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;
}

其他提示

我在做我的应用程序类似的东西。

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

没有足够的因缘尚未对答案进行评论,但是我已经和上面bbrandons代码很好的成功。

我确实得到了相当数量的警告的控制台上,虽然每行关于字节设置不是足够高的 - 阅读的答案,这样的帖子我最终将其设置为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;
}

我不知道如何ICO文件工作的话,那么也许有可能轻易地提取16×16的图像,并利用这一点,但我不知道怎么样。

据我所知,这是不可能在一个通用的UITableViewCell设置图像上的任何有用的属性。最简单的方法(仍然凌乱,特别是如果你是新来的吧)规模都下降到16×16。将继承UITableViewCell,给它一个UIImageView,总是尺寸的图像以16×16,并设置其contentMode到UIViewContentModeAspectFit

我已经修改他人的人类中,为了使所有图像占据相同的宽度(可见宽度),而无需缩放:

-(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;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top