我已经完成了将UIView转换为UIImage但我想要将UIView的任何特定部分转换为UIImage以便它可能
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer drawInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
答案 0 :(得分:3)
试试这个:
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();
您首先会从UIImage
保存原始UIView
,然后使用上述方法裁剪它。 someRect
是原始UIView
所需的矩形区域。
答案 1 :(得分:1)
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;