如何在iOS中裁剪大图像的中心部分?

Flexsin Flex

当从中心裁剪图像时,裁剪图像将采用源图像的宽高比,但是根据我的要求,宽高比将随着新的裁剪尺寸而变化。

我想使用新的长宽比获得图像的确切中心部分。例如,一张大图像的尺寸为(320 * 480),然后我想要裁剪图像的中心部分的尺寸为(100,100),长宽比也将为100 * 100 ,不需要外部白色或黑色部分,并且图像质量很高。

裁剪功能:

- (UIImage *)cropImage:(UIImage*)image andFrame:(CGRect)rect {

    //Note : rec is nothing but the image frame which u want to crop exactly.

    rect = CGRectMake(rect.origin.x*image.scale,
                      rect.origin.y*image.scale,
                      rect.size.width*image.scale,
                      rect.size.height*image.scale);

    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
    UIImage *result = [UIImage imageWithCGImage:imageRef
                                          scale:image.scale
                                    orientation:image.imageOrientation];
    CGImageRelease(imageRef);
    return result;
}

请帮我。

行星黑客

这可能会运行

- (UIImage *)imageByCroppingImage:(UIImage *)image toSize:(CGSize)size
{
    // not equivalent to image.size (which depends on the imageOrientation)!
    double refWidth = CGImageGetWidth(image.CGImage);
    double refHeight = CGImageGetHeight(image.CGImage);

    double x = (refWidth - size.width) / 2.0;
    double y = (refHeight - size.height) / 2.0;

    CGRect cropRect = CGRectMake(x, y, size.height, size.width);
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);

    UIImage *cropped = [UIImage imageWithCGImage:imageRef scale:0.0 orientation:self.imageOrientation];
    CGImageRelease(imageRef);

    return cropped;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章