【发布时间】:2013-04-04 13:21:21
【问题描述】:
是否有一种简单的方法可以选择 UIImage 实例的区域,然后从中创建新图像?我尝试使用平移手势识别器来获取用户选择的坐标。但是有了这个选项,现在有可见的反馈,我还必须转换坐标。是否有一个不错的插件或最佳实践可以做到这一点?
【问题讨论】:
标签: ios cocoa-touch cocoa core-graphics
是否有一种简单的方法可以选择 UIImage 实例的区域,然后从中创建新图像?我尝试使用平移手势识别器来获取用户选择的坐标。但是有了这个选项,现在有可见的反馈,我还必须转换坐标。是否有一个不错的插件或最佳实践可以做到这一点?
【问题讨论】:
标签: ios cocoa-touch cocoa core-graphics
试试这个:
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);
【讨论】: