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)
所以我的问题是,哪种方式更好,为什么?
| 归档时间: |
|
| 查看次数: |
6290 次 |
| 最近记录: |