- (UIImage *)convertImageWithView:(UIView *)convertView
{
CGFloat scale = [UIScreen mainScreen].scale;
UIGraphicsBeginImageContextWithOptions(convertView.bounds.size, NO, scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
[convertView.layer renderInContext:context];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGFloat x = convertView.frame.origin.x * scale;
CGFloat y = convertView.frame.origin.y * scale;
CGFloat w = convertView.frame.size.width * scale;
CGFloat h = convertView.frame.size.height * scale;
CGRect dianRect = CGRectMake(x, y, w, h);
CGImageRef sourceImageRef = [image CGImage];
CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, dianRect);
UIImage * newImage = [UIImage imageWithCGImage:newImageRef scale:scale orientation:UIImageOrientationUp];
return newImage;
}
该代码段描述了一个方法,用于将UIView的内容转换为UIImage,并且能截取指定的矩形区域。它首先创建一个图形上下文,然后将UIView的layer渲染到这个上下文中,获取UIImage,最后在图像中提取指定的矩形区域生成新的UIImage。
1773

被折叠的 条评论
为什么被折叠?



