繁体   English   中英

是否有可能将UIView的特定区域转换为iPhone中的UIImage

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM