Để tất cả các bạn các chuyên gia đồ họa, tôi đã tự hỏi mà một trong hai phương pháp này là tốt hơn cho thay đổi kích thước một UIImage:Phương pháp nào để thay đổi kích cỡ UIImage là tốt hơn và tại sao?
Người đầu tiên tôi đã đi qua là đơn giản và phổ biến và được như sau:
-(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;
}
phương pháp thứ hai tôi thấy tại liên kết này http://iphonesdksnippets.com/post/2009/05/06/Resize-image-and-keep-aspect-ratio.aspx và dường như để thực hiện tương tự như trên, nhưng có nhiều phức tạp hơn (tôi không thực sự hiểu những gì đang xảy ra trong nó):
-(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;
}
vì vậy, m câu hỏi là, cách nào tốt hơn và tại sao?
Tiếp cận mức thấp = duy trì chất lượng tốt hơn sau khi thay đổi kích thước? @tom –
Tôi đoán phương thức drawInRect của UIImage sẽ sử dụng CGContextDrawImage trong nội bộ ... – Tom