调整UIImage大小的最简单方法是什么? [英] The simplest way to resize an UIImage?

查看:125
本文介绍了调整UIImage大小的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iPhone应用程序中,我用相机拍照,然后我想将其调整为290 * 390像素。我正在使用此方法调整图像大小:

In my iPhone app, I take a picture with the camera, then I want to resize it to 290*390 pixels. I was using this method to resize the image :

UIImage *newImage = [image _imageScaledToSize:CGSizeMake(290, 390)
                         interpolationQuality:1];    

它完美无缺,但它是一个无证件的功能,所以我不能再使用iPhone OS4了。

It works perfectly, but it's an undocumented function, so I can't use it anymore with iPhone OS4.

那么...调整UIImage大小的最简单方法是什么?

So... what is the simplest way to resize an UIImage ?

推荐答案

最简单的方法是设置 UIImageView 的框架,将 contentMode 设置为其中一个调整大小选项。

The simplest way is to set the frame of your UIImageView and set the contentMode to one of the resizing options.

或者,如果您确实需要,可以使用此实用程序方法调整图像大小:

Or you can use this utility method, if you actually need to resize an image:

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
    // Pass 1.0 to force exact pixel size.
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}

用法示例:

#import "MYUtil.h"
…
UIImage *myIcon = [MYUtil imageWithImage:myUIImageInstance scaledToSize:CGSizeMake(20, 20)];

这篇关于调整UIImage大小的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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