在平常的开发中,会用到截屏过程,也就是将UIView指定区域转换成图片,下面就是实现代码:
-(UIImage*) imageFromView:(UIView *) v rect:(CGRect) rect{
UIGraphicsBeginImageContextWithOptions(v.frame.size, NO, 1.0); //NO,YES 控制是否透明
[v.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
CGRect myImageRect = rect;
CGImageRef imageRef = image.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef,myImageRect );
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, myImageRect, subImageRef);
UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
CGImageRelease(subImageRef);
UIGraphicsEndImageContext();
return smallImage;
}