试图将我的UIImage裁剪为1:1的宽高比(正方形),但它会不断放大图像,使其模糊。为什么? [英] Trying to crop my UIImage to a 1:1 aspect ratio (square) but it keeps enlarging the image causing it to be blurry. Why?

查看:111
本文介绍了试图将我的UIImage裁剪为1:1的宽高比(正方形),但它会不断放大图像,使其模糊。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于UIImage,我正试图将它变成一个正方形。只需将一些最大的尺寸切掉,使其纵横比为1:1。

Given a UIImage, I'm trying to make it into a square. Just chop some of the largest dimension off to make it 1:1 in aspect ratio.

UIImage *pic = [UIImage imageNamed:@"pic"];
CGFloat originalWidth = pic.size.width;
CGFloat originalHeight = pic.size.height;

float smallestDimension = fminf(originalWidth, originalHeight);

CGRect square = CGRectMake(0, 0, smallestDimension, smallestDimension);
CGImageRef imageRef = CGImageCreateWithImageInRect([pic CGImage], square);

UIImage *squareImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

UIImageView *imageView = [[UIImageView alloc] initWithImage:squareImage];
imageView.frame = CGRectMake(100, 100, imageView.bounds.size.width, imageView.bounds.size.height);

[self.view addSubview:imageView];

但这就是结果:

何时应该看起来像这样,但只是稍微窄一点。

When it should look like this, but just a little narrower.

这是为什么?图像为 pic (150x114)/ pic @ 2x (300x228)。

Why is this? The images are pic(150x114) / pic@2x(300x228).

推荐答案

问题在于你是在混合逻辑和像素大小。在非视网膜设备上,这两个是相同的,但在视网膜设备上(如你的情况),像素大小实际上是逻辑大小的两倍。

The problem is you're mixing up logical and pixel sizes. On non retina devices these two are the same, but on retina devices (like in your case) the pixel size is actually double the logical size.

通常,在设计你的时候GUI,您可以随时思考逻辑大小和坐标,iOS(或OS X)将确保视网膜屏幕上的所有内容都加倍。但是,在某些情况下,特别是在自己创建图像时,您必须明确指定您的大小。

Usually, when designing your GUI, you can always just think in logical sizes and coordinates, and iOS (or OS X) will make sure, that everything is doubled on retina screens. However, in some cases, especially when creating images yourself, you have to explicitly specify what size you mean.

UIImage size 方法返回逻辑大小。例如,这是非视网膜屏幕上的分辨率。这就是为什么 CGImageCreateWithImageInRect 只能从图像的左上角创建一个新图像。

UIImage's size method returns the logical size. That is the resolution on non-retina screens for instance. This is why CGImageCreateWithImageInRect will only create an new image, from the upper left half of the image.

乘以你的逻辑大小与图像的比例(非视网膜设备上1,视网膜设备上2):

Multiply your logical size with the scale of the image (1 on non-retina devices, 2 on retina devices):

CGFloat originalWidth = pic.size.width * pic.scale;
CGFloat originalHeight = pic.size.height * pic.scale;

这将确保新图像是从整个高度(或宽度)创建的原始图像。现在,剩下的一个问题是,当你使用

This will make sure, that the new image is created from the full height (or width) of the original image. Now, one remaining problem is, that when you create a new UIImage using

UIImage *squareImage = [UIImage imageWithCGImage:imageRef];

iOS会认为,这是一个常规的非视网膜图像,它会显示两倍大正如你所料。要解决此问题,您必须在创建 UIImage 时指定比例:

iOS will think, this is a regular, non-retina image and it will display it twice as large as you would expect. To fix this, you have to specify the scale when you create the UIImage:

UIImage *squareImage = [UIImage imageWithCGImage:imageRef 
                                           scale:pic.scale 
                                     orientation:pic.imageOrientation];

这篇关于试图将我的UIImage裁剪为1:1的宽高比(正方形),但它会不断放大图像,使其模糊。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆