[英]Is it possible to convert particular area of UIView into UIImage in iPhone
我已经完成了将UIView转换为UIImage但我希望将UIView的任何特定部分转换为UIImage,这样才有可能
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer drawInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
尝试这个:
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();
首先,您将从UIView
保存原始UIImage
,然后使用上述方法裁剪它。 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.