我的要求是,当我处理任何尺寸的图像时,该图像应自动裁剪为 320x240 的尺寸,无需任何拉伸(stretch)。如何为此功能编写裁剪方法?我的意思是我有各种尺寸的图像列表,如果我从此列表中选择一个图像,它将自动转换为尺寸 320x 240。我怎样才能实现它。请给我建议。
现在我使用以下编码: http://www.abdus.me/ios-programming-tips/resize-image-in-ios/
但是生成的图像质量不佳,变得模糊。您能告诉我如何克服这个问题吗?
最佳答案
对于裁剪 - 图像部分:
- (UIImage*) getSubImageFrom: (UIImage*) img WithRect: (CGRect) rect {
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
// translated rectangle for drawing sub image
CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, img.size.width, img.size.height);
// clip to the bounds of the image context
// not strictly necessary as it will get clipped anyway?
CGContextClipToRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height));
// draw image
[img drawInRect:drawRect];
// grab image
UIImage* subImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return subImage;
}
用于调整大小 -
将UIImageView的框架设置为320x240,然后imageView.contentMode = UIViewContentModeScaleAspectFit;
关于ios - 如何将任意尺寸的图像裁剪为特定尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24239815/