在 UITableViewCell 图像上设置 UIImage 尺寸

发布于 2024-07-22 09:20:51 字数 788 浏览 11 评论 0原文

我有一个标准的 UITableViewCell ,我在其中使用文本和图像属性来显示 favicon.ico 和标签。 在大多数情况下,由于 UIImage 支持 ICO 格式,因此效果非常好。 但是,某些网站(例如 Amazon.com 等)有 favicon.ico 利用 ICO 格式在同一文件中存储多个大小的能力。 亚马逊存储四种不同尺寸,最大尺寸为 48x48。

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

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

有没有人有什么建议? (我也在官方论坛询问,因为我已经沉迷了一天多了如果那里已经发布了解决方案,我也会将其放在这里。)

I've got a standard UITableViewCell where I'm using the text and image properties to display a favicon.ico and a label. For the most part, this works really well since UIImage supports the ICO format. However, some sites (like Amazon.com say) 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.

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#).

In case I wasn't clear in what I'm looking for, ideally the advice would center around setting the dimensions of the UIImage so that the 48x48 version would scale down to 16x16 or a method to tell UIImage to 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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

恋你朝朝暮暮 2024-07-29 09:20:51

以下是用户“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;
}

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

+ (UIImage *)scale:(UIImage *)image toSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}
无法言说的痛 2024-07-29 09:20:51

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

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

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.

CGRect sz = CGRectMake(0.0f, 0.0f, 16, 16);
UIImage *resized = resizedImage(image, sz);
眼角的笑意。 2024-07-29 09:20:51

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

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

例如:

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

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

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 post I ended up setting it to 0, which lets the system determine the right value.

eg:

CGContextRef bitmap = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, CGColorSpaceCreateDeviceRGB(), alphaInfo);
情域 2024-07-29 09:20:51

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

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

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;
}
长亭外,古道边 2024-07-29 09:20:51

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

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

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.

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.

落叶缤纷 2024-07-29 09:20:51

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

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文