OGeek|极客世界-中国程序员成长平台

标题: ios - 如何将任意大小的图像裁剪到特定尺寸? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:24
标题: ios - 如何将任意大小的图像裁剪到特定尺寸?

我的要求是当我处理任何尺寸的图像时,该图像应自动裁剪为 320x240 的尺寸而无需任何拉伸(stretch)。如何为此功能编写裁剪方法?我的意思是我有各种尺寸的图像列表,如果我从这个列表中选择一个图像,它将自动转换为 320x 240 的尺寸。我该如何实现它。请给我建议。

现在我使用以下编码: http://www.abdus.me/ios-programming-tips/resize-image-in-ios/

但是生成的图像质量不好,它会变得模糊。你能告诉我如何解决这个问题。



Best Answer-推荐答案


对于裁剪 - 图像部分:

- (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的frame放到320x240然后imageView.contentMode = UIViewContentModeScaleAspectFit;

关于ios - 如何将任意大小的图像裁剪到特定尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24239815/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4