如何选择一个UIImage的区域,然后创建一个新的区域?
问题描述:
是有选择的UIImage实例的一个区域,然后从它创建一个新的形象的一种简单的方法?我尝试使用平移手势识别器获取用户选择的坐标。但有了这个选项,现在可以看到反馈,我也必须转换坐标。是否有一个很好的插件或最佳做法?如何选择一个UIImage的区域,然后创建一个新的区域?
答
试试这个:
UIGraphicsBeginImageContext(self.view.bounds.size);
// retrieve the current graphics context
CGContextRef context = UIGraphicsGetCurrentContext();
// render view into context
[self.view.layer renderInContext:context];
// create image from context
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
image=[self cropImage:image];
UIGraphicsEndImageContext();
- (UIImage *)cropImage:(UIImage *)oldImage {
CGSize imageSize = oldImage.size;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageSize.width,imageSize.height - 150),NO,0.); //set your height and width
[oldImage drawAtPoint:CGPointMake(0, -80) blendMode:kCGBlendModeCopy alpha:1.];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return croppedImage;
}
答
像这样:
CGImageRef imageRef = CGImageCreateWithImageInRect([bigImage CGImage], cropRect);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);