objective-c UITableViewCell 的 imageView 适合 40x40

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1055495/
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:47:40  来源:igfitidea点击:

UITableViewCell's imageView fit to 40x40

objective-ciphoneuitableviewuiimageview

提问by slatvick

I use the same big images in a tableView and detailView. Need to make imageView filled in 40x40 when an imags is showed in tableView, but stretched on a half of a screen. I played with several properties but have no positive result:

我在 tableView 和 detailView 中使用相同的大图像。当在 tableView 中显示图像但在屏幕的一半上拉伸时,需要使 imageView 填充为 40x40。我玩了几个属性,但没有积极的结果:

[cell.imageView setBounds:CGRectMake(0, 0, 50, 50)];
[cell.imageView setClipsToBounds:NO];
[cell.imageView setFrame:CGRectMake(0, 0, 50, 50)];
[cell.imageView setContentMode:UIViewContentModeScaleAspectFill];

I am using SDK 3.0 with build in "Cell Objects in Predefined Styles".

我正在使用带有内置“预定义样式的单元格对象”的 SDK 3.0。

回答by PapaSmurf

I put Ben's code as an extension in my NS-Extensions file so that I can tell any image to make a thumbnail of itself, as in:

我将 Ben 的代码作为扩展名放在我的 NS-Extensions 文件中,以便我可以告诉任何图像制作自己的缩略图,如下所示:

UIImage *bigImage = [UIImage imageNamed:@"yourImage.png"];
UIImage *thumb = [bigImage makeThumbnailOfSize:CGSizeMake(50,50)];

Here is .h file:

这是 .h 文件:

@interface UIImage (PhoenixMaster)
- (UIImage *) makeThumbnailOfSize:(CGSize)size;
@end

and then in the NS-Extensions.m file:

然后在 NS-Extensions.m 文件中:

@implementation UIImage (PhoenixMaster)
- (UIImage *) makeThumbnailOfSize:(CGSize)size
{
    UIGraphicsBeginImageContextWithOptions(size, NO, UIScreen.mainScreen.scale);
    // draw scaled image into thumbnail context
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *newThumbnail = UIGraphicsGetImageFromCurrentImageContext();        
    // pop the context
    UIGraphicsEndImageContext();
    if(newThumbnail == nil) 
        NSLog(@"could not scale image");
    return newThumbnail;
}

@end

回答by Ben Lachman

I cache a thumbnail version since using large images scaled down on the fly uses too much memory.

我缓存了一个缩略图版本,因为使用动态缩小的大图像会占用太多内存。

Here's my thumbnail code:

这是我的缩略图代码:

- (UIImage *)thumbnailOfSize:(CGSize)size {
    if( self.previewThumbnail )
        return self.previewThumbnail; // returned cached thumbnail

    UIGraphicsBeginImageContext(size);

    // draw scaled image into thumbnail context
    [self.preview drawInRect:CGRectMake(0, 0, size.width, size.height)];

    UIImage *newThumbnail = UIGraphicsGetImageFromCurrentImageContext();    

    // pop the context
    UIGraphicsEndImageContext();

    if(newThumbnail == nil) 
        NSLog(@"could not scale image");

    self.previewThumbnail = newThumbnail;

    return self.previewThumbnail;
}

Just make sure you properly clear the cached thumbnail if you change your original image (self.preview in my case).

如果您更改原始图像(在我的情况下为 self.preview),请确保正确清除缓存的缩略图。

回答by alex_c

I have mine wrapped in a UIView and use this code:

我将我的包裹在 UIView 中并使用以下代码:

imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
[self addSubview:imageView];
imageView.frame = self.bounds;

(self is the wrapper UIView, with the dimensions I want - I use AsyncImageView).

(self 是包装器 UIView,具有我想要的尺寸 - 我使用AsyncImageView)。

回答by guiller

I thought Ben Lachman's suggestion of generating thumbnails in advance rather than on the fly was smart, so I adapted his code so it could handle a whole array and to make it more portable (no hard-coded property names).

我认为 Ben Lachman 提前生成缩略图而不是即时生成缩略图的建议是明智的,因此我调整了他的代码,以便它可以处理整个数组并使其更具可移植性(没有硬编码的属性名称)。

- (NSArray *)arrayOfThumbnailsOfSize:(CGSize)size fromArray:(NSArray*)original {
    NSMutableArray *temp = [NSMutableArray arrayWithCapacity:[original count]];
    for(UIImage *image in original){
        UIGraphicsBeginImageContext(size);
        [image drawInRect:CGRectMake(0,0,size.width,size.height)];
        UIImage *thumb = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        [temp addObject:thumb];
    }
    return [NSArray arrayWithArray:temp];
}

回答by Wolfcow

you might be able to use this?

你可以用这个吗?

yourTableViewController.rowImage = [UIImage imageNamed:@"yourImage.png"];

and/or

和/或

cell.image = yourTableViewController.rowImage;

and if your images are already 40x40 then you shouldn't have to worry about setting bounds and stuff... but, i'm also new to this, so, i wouldn't know, haven't played around with Table View row/cell images much

如果您的图像已经是 40x40,那么您不必担心设置边界和其他东西...但是,我也是新手,所以,我不知道,还没有玩过 Table View 行/cell 图像很多

hope this helps.

希望这可以帮助。

回答by Jonah

I was able to make this work using interface builder and a tableviewcell. You can set the "Mode" properties for an image view to "Aspect Fit". I'm not sure how to do this programatically.

我能够使用界面构建器和 tableviewcell 来完成这项工作。您可以将图像视图的“模式”属性设置为“Aspect Fit”。我不确定如何以编程方式执行此操作。

回答by Jonah

Try setting UIImageView.autoresizesSubviewsand/or UIImageView.contentStretch.

尝试设置UIImageView.autoresizesSubviews和/或UIImageView.contentStretch.