调整UIImage的方法哪个更好,为什么?

Ser*_*nce 10 iphone graphics cocoa-touch core-graphics ios

对于你们所有的图形专家,我想知道这两种方法中哪一种更适合调整UIImage的大小:

我遇到的第一个是简单而流行的,如下:

-(UIImage *)resizeImage:(UIImage *)image width:(CGFloat)resizedWidth height:(CGFloat)resizedHeight
{
    UIGraphicsBeginImageContext(CGSizeMake(resizedWidth ,resizedHeight));
    [image drawInRect:CGRectMake(0, 0, resizedWidth, resizedHeight)];
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return result;
}
Run Code Online (Sandbox Code Playgroud)

我在这个链接http://iphonesdksnippets.com/post/2009/05/06/Resize-image-and-keep-aspect-ratio.aspx找到的第二种方法似乎与上面的相同,但是很多更复杂(我真的不明白其中发生了什么):

-(UIImage *)resizeImage:(UIImage *)image width:(CGFloat)resizedWidth height:(CGFloat)resizedHeight
 {
    CGImageRef imageRef = [image CGImage];

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef bitmap = CGBitmapContextCreate(NULL, resizedWidth, resizedHeight, 8, 4 * resizedWidth, colorSpace, kCGImageAlphaPremultipliedFirst);
    CGContextDrawImage(bitmap, CGRectMake(0, 0, resizedWidth, resizedHeight), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage *result = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);
    CGImageRelease(ref);

    return result;  
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,哪种方式更好,为什么?

Tom*_*Tom 0

这是一种高级方法和低级方法。

哪个更好?取决于你的目标。

高级方法更具可读性(对我来说),也许低级方法快一纳秒(没有测试)。

很难判断算法...