ios - 如何将任意尺寸的图像裁剪为特定尺寸?

标签 ios iphone objective-c uiimageview crop

我的要求是,当我处理任何尺寸的图像时,该图像应自动裁剪为 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/

相关文章:

ios - 将两个数组中的所有字符串放入另一个数组

objective-c - 具有自定义 getter 的只读属性是否会合成一个实例变量?

ios - 用于自定义拇指位置和拇指图像的 UISlider 子类?

iphone - AVAudioPlayer isPlaying 始终返回 null

iphone - 当用户选择不购买 iOS InApp Purchase 中的项目时,如何自定义错误处理?

ios - 从 iPhone SDK 中的 UIImage 获取黑白图像?

iphone - RemoveAllObjects 等效于 NSArray

ios - 计算多个 uitextfields 结果的最佳方法

ios - 无法转换类型的值 - Swift

iphone - 使 UIView 变暗