我正在使用以下代码裁剪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);
任何解决方案?
答案 0 :(得分:0)
逻辑上,对于图像的裁剪,只有尺寸是相关的。因此,您必须相应地调整width
和height
。框架的x
和y
坐标只会改变位置,而不会改变大小。
答案 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;
}