我正在谷歌搜索过去几个小时并获得解决方案来裁剪图像,但我很困惑,想知道这两者之间有什么区别?
问题:我想从图像内部裁剪矩形图像,我已检测到边界。
第一:
CGRect rect = CGRectMake(0,0,320, 460);
// Create bitmap image from original image data,
// using rectangle to specify desired crop area
CGImageRef imageRef = CGImageCreateWithImageInRect([imageView.image CGImage], rect);
UIImage *imgs = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
第二:
CGRect rect = [backView bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[backView.layer renderInContext:context];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
答案 0 :(得分:2)
第二种方法更为通用,因为您的backView
不需要像UIImageView
那样,就像第一种情况一样。
换句话说,CGImageCreateWithImageInRect
要求您开始使用CGImage:
CGImageRef imageRef = CGImageCreateWithImageInRect([imageView.image CGImage], rect);
另一方面,使用renderInContext
可以将任何视图呈现为图像:
[backView.layer renderInContext:context];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();