objective-c 在 UITableViewCell 图像上设置 UIImage 尺寸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/941604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:39:04  来源:igfitidea点击:

Setting UIImage dimensions on UITableViewCell image

iphoneobjective-ccocoa-touchuikitfavicon

提问by bbrown

I've got a standard UITableViewCellwhere I'm using the text and image properties to display a favicon.icoand a label. For the most part, this works really well since UIImagesupports the ICO format. However, some sites (like Amazon.comsay) have favicon.icos that make use of the ICO format's ability to store multiple sizes in the same file. Amazon stores four different sizes, all the way up to 48x48.

我有一个标准UITableViewCell,我使用 text 和 image 属性来显示 afavicon.ico和一个标签。在大多数情况下,这非常有效,因为它UIImage支持 ICO 格式。但是,某些站点(如Amazon.com所说)具有favicon.ico利用 ICO 格式在同一文件中存储多个大小的能力的 s。亚马逊存储四种不同的尺寸,最高可达 48x48。

This results in most images being 16x16 except for a few that come in at 32x32 or 48x48 and make everything look terrible. I have searched here, the official forum, the documentation, and elsewhere without success. I have tried everything that I could think of to constrain the image size. The only thing that worked was an undocumented method, which I'm not about to use. This is my first app and my first experience with Cocoa (came from C#).

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

In case I wasn't clear in what I'm looking for, ideally the advice would center around setting the dimensions of the UIImageso that the 48x48 version would scale down to 16x16 or a method to tell UIImageto use the 16x16 version present in the ICO file. I don't necessarily need code: just a suggestion of an approach would do me fine.

如果我不清楚我在寻找什么,理想情况下,建议将围绕设置尺寸,UIImage以便 48x48 版本缩小到 16x16 或告诉UIImage使用 ICO 中存在的 16x16 版本的方法文件。我不一定需要代码:只要提出一种方法的建议就可以了。

Does anyone have any suggestions? (I asked in the official forum as wellbecause I've sunk more than a day into this already. If a solution is posted there, I'll put it here as well.)

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

回答by bbrown

Here's what user "bcd" over at the official forums postedthat ended up solving the problem (with a slight modification by myself to make it static for inclusion into a set of utility methods):

这是官方论坛上用户“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;
}

回答by Brandon Schlenker

I'm doing something similar in my app.

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

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

Then create a new image constraining the proportions. Replacing "image" with the ICO file you get from Amazon.

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

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

回答by crafterm

Don't have enough karma yet to comment on answers, but I had good success with bbrandons code above.

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

I did get a fair number of warnings on the console though regarding bytes per row setting not being high enough - after reading answers to this postI ended up setting it to 0, which lets the system determine the right value.

我确实在控制台上收到了相当多的警告,尽管每行设置的字节数不够高 - 在阅读这篇文章的答案后,我最终将其设置为 0,这让系统确定正确的值。

eg:

例如:

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

回答by Chris

Here's how i did it. This technique takes care of moving the text and detail text labels appropriately to the left:

这是我如何做到的。此技术负责将文本和详细文本标签适当地向左移动:

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

回答by LolaRun

I have modified others' people category, in order to make all images occupy the same width(visible width), WITHOUT SCALING:

我修改了其他人的人物类别,以使所有图像占据相同的宽度(可见宽度),无需缩放:

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

回答by zem

I don't know anything about how ico files work, so maybe it's possible to handily extract the 16x16 image and use that, but I have no idea how.

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

As far as I'm aware, it's not possible to set any useful properties on the image in a generic UITableViewCell. The simplest way (still messy, especially if you're new to it) to scale everything down to 16x16 would be to subclass UITableViewCell, give it a UIImageView, always size the image view to 16x16, and set its contentMode to UIViewContentModeAspectFit.

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