问题描述
我有一个16x16像素的图像,我想显示在UIImageView。到目前为止,没有问题,但是16x16有点小,所以我想调整图像视图到32x32,从而也缩放图像。
但是我不能让它工作,它总是显示与16x16的图像,无论我试试。我googled了很多,并发现很多片段这里在Stack Overflow,但它仍然不工作。
这里是我的代码到目前为止:
[[cell.imageView layer] setMagnificationFilter:kCAFilterNearest];
[cell.imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[cell.imageView setClipsToBounds:NO];
[cell.imageView setFrame:CGRectMake(0,0,32,32)];
[cell.imageView setBounds:CGRectMake(0,0,32,32)];
[cell.imageView setImage:image];
我不想创建一个新的32x32像素图像,因为我已经有一些内存问题设备和创建两个图像,而不是只有一个看起来像一个非常糟糕的方法对我来说(图像可以完美地缩放,如果他们失去质量没有关系)。
cell.imageView.image = cellImage;
//self.rowWidth是所需的宽度
//self.rowHeight是所需的高度
CGFloat widthScale = self.rowWidth / cellImage.size.width;
CGFloat heightScale = self.rowHeight / cellImage.size.height;
//这行就行了!
cell.imageView.transform = CGAffineTransformMakeScale(widthScale,heightScale);
I have a 16x16 pixel image that I want to display in an UIImageView. So far, no problem, however 16x16 is a bit small so I want to resize the image view to 32x32 and thus also scale the image up.But I can't get it to work, it always shows the image with 16x16, no matter what I try. I googled a lot, and found many snippets here on Stack Overflow, but its still doesn't work.Here is my code so far:
[[cell.imageView layer] setMagnificationFilter:kCAFilterNearest];
[cell.imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[cell.imageView setClipsToBounds:NO];
[cell.imageView setFrame:CGRectMake(0, 0, 32, 32)];
[cell.imageView setBounds:CGRectMake(0, 0, 32, 32)];
[cell.imageView setImage:image];
I don't want to create a new 32x32 pixel image because I already have some memory problems on older devices and creating two images instead of having just one looks like a very bad approach to me (the images can be perfectly scaled and it doesn't matter if they lose quality).
I have successfully made it using CGAffineTransformMakeScale!
cell.imageView.image = cellImage;
//self.rowWidth is the desired Width
//self.rowHeight is the desired height
CGFloat widthScale = self.rowWidth / cellImage.size.width;
CGFloat heightScale = self.rowHeight / cellImage.size.height;
//this line will do it!
cell.imageView.transform = CGAffineTransformMakeScale(widthScale, heightScale);
这篇关于在UITableViewCell中调整UIImageView的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!