1

我已经完成了将 UIView 转换为 UIImage 的操作,但我希望将 UIView 的任何特定部分转换为 UIImage 以便有可能

UIGraphicsBeginImageContext(view.bounds.size);
[view.layer drawInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
4

2 回答 2

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();

您首先将从中保存原件UIImageUIView然后使用上述方法对其进行裁剪。someRect是您想要的原始矩形区域UIView

于 2012-06-12T04:48:15.670 回答
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;
于 2012-06-12T05:09:30.523 回答