目标C:如何修复图像的宽高比(而不是更改为适合图像视图帧) [英] Objective C: How to fix aspect ratio of image (and not change to fit imageview frame)

查看:114
本文介绍了目标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在手机上有一个原始图像如下(非方形图像):

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

通过移动和缩放页面选择iphone上的图像裁剪图像后,结果如下:宽高比仍然保留在这里)

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)

编辑:使用示例代码更新

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

通过添加代码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.

推荐答案

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天全站免登陆