ios 目标 C:如何修复图像的纵横比(而不是更改以适应图像视图框架)

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

Objective C: How to fix aspect ratio of image (and not change to fit imageview frame)

objective-ciosuiimagepickercontrolleraspect-ratio

提问by Zhen

I am using the UIImagePickerController to select images for my app. However I am facing some challenges with regards to maintaining aspect ratio of the selected images

我正在使用 UIImagePickerController 为我的应用程序选择图像。但是,我在保持所选图像的纵横比方面面临一些挑战

For example,

例如,

I have a raw image on the phone as follows (not square image):

我在手机上有一个原始图像如下(不是方形图像):

enter image description here

在此处输入图片说明

After selecting the image on iphone via the 'move and scale' page to crop image, the result is as follows: (aspect ratio is still maintained here)

通过'move and scale'页面选择iphone上的图片裁剪图片后,结果如下:(这里还是保持纵横比)

enter image description here

在此处输入图片说明

My app will display the selected image in a UIImageView with a square frame (200 x 200). After setting the selected image to the UIImageView (UIImageView.image = selectedImage), the image's aspect ratio is not maintained but instead followed the frame of the UIImageView: (The image looks skewed now in my UIImageView):

我的应用程序将在带有方形框架 (200 x 200) 的 UIImageView 中显示所选图像。将所选图像设置为 UIImageView (UIImageView.image = selectedImage) 后,图像的纵横比没有保持,而是遵循 UIImageView 的框架:(图像现在在我的 UIImageView 中看起来偏斜):

enter image description here

在此处输入图片说明

Is there any way to maintain the aspect ratio of the image in this case?

在这种情况下,有没有办法保持图像的纵横比?

EDIT: Update expected result

编辑:更新预期结果

After some thinking, I realize that the best way to resolve my issue is to ensure that for such images (non square), I need to 'convert' them into square images i.e. fill the empty spaces on top and bottom of images to make a square e.g.

经过一番思考,我意识到解决我的问题的最佳方法是确保对于此类图像(非方形),我需要将它们“转换”为方形图像,即填充图像顶部和底部的空白区域以制作一个方形例如

Expected image: (with the top and bottom borders added)

预期图像:(添加了顶部和底部边框)

enter image description here

在此处输入图片说明

EDIT: Update with sample code

编辑:使用示例代码更新

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo 
{

    self.imageView.contentMode = UIViewContentModeScaleAspectFill;

    self.imageView.image = image;
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];

    //Compress image
    UIImage *image = [self scaleImage:img toSize:CGSizeMake(612.0,612.0)];

    self.imageData = UIImageJPEGRepresentation(image, 0.75);
}

By adding the code "self.imageView.contentMode = UIViewContentModeScaleAspectFill;", I was able to obtain the square image in the uiimageview

通过添加代码“self.imageView.contentMode = UIViewContentModeScaleAspectFill;”,我能够在 uiimageview 中获取方形图像

enter image description here

在此处输入图片说明

But it seems like the original image was still not a square image, hence the skewing problem will still occur when I do "UIImage *image = [self scaleImage:img toSize:CGSizeMake(612.0,612.0)];". Is my assumption correct? Anyway to mitigate this issue as I will still need to display the 'compressed' image elsewhere in the app.

但看起来原始图像仍然不是方形图像,因此当我执行“UIImage *image = [self scaleImage:img toSize:CGSizeMake(612.0,612.0)];”时仍然会出现倾斜问题。我的假设正确吗?无论如何要缓解这个问题,因为我仍然需要在应用程序的其他地方显示“压缩”图像。

回答by edo42

Doing myImageView.contentMode = UIViewContentModeScaleAspectFit;isn't enough because it changes the aspect ratio of the image in the way Zhen wants, but it doesn't change the source image, which is what Zhen needs. The only way to do that is to use the UIGraphicsImageContext as explained there: Resize UIImage with aspect ratio?

这样做myImageView.contentMode = UIViewContentModeScaleAspectFit;还不够,因为它以Zhen想要的方式改变了图像的纵横比,但它不会改变源图像,这正是Zhen需要的。做到这一点的唯一方法是使用 UIGraphicsImageContext,如此处所述: Resize UIImage with aspect ratio?

回答by Eimantas

You should adjust your UIImageView's content mode:

你应该调整你UIImageView的内容模式:

myImageView.contentMode = UIViewContentModeScaleAspectFit;

Just for the kicks - check documentation on UIViewContentModeconstants.

只是为了踢 - 检查关于UIViewContentMode常量的文档。

回答by aroth

You can set the contentModeproperty on your UIImageViewinstance, like:

您可以contentModeUIImageView实例上设置属性,例如:

myImageView.contentMode = UIViewContentModeScaleAspectFit;

This will force it to preserve your image's aspect-ratio when scaling. There are also a number of other values you can set here to produce other behaviors, as described in the reference documentation.

这将强制它在缩放时保留图像的纵横比。您还可以在此处设置许多其他值以产生其他行为,如参考文档 中所述