目标 C:如何修复图像的纵横比(而不是更改以适应图像视图框架) [英] Objective C: How to fix aspect ratio of image (and not change to fit imageview frame)

查看:24
本文介绍了目标 C:如何修复图像的纵横比(而不是更改以适应图像视图框架)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

例如

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

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

在iphone上通过'move and scale'页面选中图片裁剪图片后,结果如下:(这里还是保持长宽比)

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)

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

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

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

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

更新预期结果

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

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)

使用示例代码更新

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

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

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

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

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.

推荐答案

Doing myImageView.contentMode = UIViewContentModeScaleAspectFit; 是不够的,因为它以Zhen想要的方式改变了图像的纵横比,但它不会改变源图像,这正是Zhen 所需要的.这样做的唯一方法是使用 UIGraphicsImageContext ,如此处所述:根据纵横比调整 UIImage 的大小?

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?

这篇关于目标 C:如何修复图像的纵横比(而不是更改以适应图像视图框架)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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