IOS UIView部分截取成UIimage图片格式

+(UIImage *)ClipToImageFromUIView:(UIView *)BigView CGRect:(CGRect)rect{

//    UIGraphicsBeginImageContext(BigView.bounds.size);

// 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了

UIGraphicsBeginImageContextWithOptions(BigView.bounds.size, NO, [UIScreen mainScreen].scale);

[BigView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage*pBigViewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

CGImageRef pCgImageRef = pBigViewImage.CGImage;

CGFloat pRectY = rect.origin.y*2;

CGFloat pRectWidth = rect.size.width*2;

CGFloat pRectHeight = rect.size.height*2;

CGRect pToRect = CGRectMake(0, pRectY, pRectWidth, pRectHeight);

CGImageRef imageRef = CGImageCreateWithImageInRect(pCgImageRef, pToRect);

UIImage *pToImage = [UIImage imageWithCGImage:imageRef];

CGImageRelease(imageRef);

return pToImage;

}

你可能感兴趣的:(IOS UIView部分截取成UIimage图片格式)