[英]Is it possible to convert particular area of UIView into UIImage in iPhone
I have already done to convert UIView into UIImage but i want that any particular section of the UIView be converted into UIImage so it is possible 我已经完成了将UIView转换为UIImage但我希望将UIView的任何特定部分转换为UIImage,这样才有可能
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer drawInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Try this: 尝试这个:
CGImageRef imageRef = CGImageCreateWithImageInRect([theImage CGImage], someRect);
CGFloat width = CGImageGetWidth(imageRef);
CGFloat height = CGImageGetHeight(imageRef);
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imageRef);
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
You first would save the original UIImage
from the UIView
and then crop it with the above method. 首先,您将从
UIView
保存原始UIImage
,然后使用上述方法裁剪它。 someRect
is the rectangle area you want from the original UIView
. someRect
是原始UIView
someRect
的矩形区域。
CGImageRef imageRef = CGImageCreateWithImageInRect([theImage CGImage], someRect);
CGFloat width = CGImageGetWidth(imageRef);
CGFloat height = CGImageGetHeight(imageRef);
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imageRef);
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
NSData *data= UIImagePNGRepresentation(croppedImage);
UIImage *img = [UIImage imageWithData:data];
UIGraphicsEndImageContext();
return img;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.