我一直在使用此方法将UIView
转换为UIImage
。即视图的屏幕快照 -
@interface UIView(Extended)
- (UIImage *) imageByRenderingView;
@end
@implementation UIView(Extended)
- (UIImage *)imageByRenderingView
{
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
@end
要使用它,我这样做 -
UIImage *currImage = [self.view imageByRenderingView];
这给出了整个UIView
的图像表示。现在我想要2个图像,一个是UIView
的上半部分,另一个是下半部分。我该怎么做?
答案 0 :(得分:11)
您可以使用以下代码将UIImage
拆分为两个:
CGImageRef topOfImageCG =
CGImageCreateWithImageInRect(currImage.CGImage,
CGRectMake(0,
0,
currImage.size.width,
currImage.size.height / 2.0));
UIImage *topOfImage = [UIImage imageWithCGImage:topOfImageCG];
CGImageRelease(topOfImageCG);