无法相对于Y轴裁剪UIImage

时间:2013-03-01 07:24:30

标签: iphone ios uiimageview uiimage crop

我正在使用以下代码裁剪UIImage:

UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];

CGRect rect = CGRectMake(10,10,700, 700);

CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

我能够以高度裁剪,但如果我增加了我的Y轴,它就不会被裁剪。

CGRect rect = CGRectMake(10,50,700, 700);

任何解决方案?

2 个答案:

答案 0 :(得分:0)

逻辑上,对于图像的裁剪,只有尺寸是相关的。因此,您必须相应地调整widthheight。框架的xy坐标只会改变位置,而不会改变大小。

答案 1 :(得分:0)

查看这个github库,它可以执行您想要的imageCroppedToRect

- (UIImage *)imageCroppedToRect:(CGRect)rect {
    // CGImageCreateWithImageInRect's `rect` parameter is in pixels of the image's coordinates system. Convert from points.
    CGFloat scale = self.scale;
    rect = CGRectMake(rect.origin.x * scale, rect.origin.y * scale, rect.size.width * scale, rect.size.height * scale);

    CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
    UIImage *cropped = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return cropped;
}