h文件:
@interface UIView (Screenshot)
//目前最完善
- (UIImage*) screenshotAtFrame:(CGRect)r;
@end
m文件:
//目前最完善
- (UIImage*) screenshotAtFrame:(CGRect)r
{
float scale = [UIScreen mainScreen].scale*2.0;
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, scale);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (CGRectEqualToRect(self.bounds, r)){
return theImage;
}else{
CGImageRef imageRef = theImage.CGImage;
r.origin.x = r.origin.x * scale;
r.origin.y = r.origin.y * scale;
r.size.height = r.size.height * scale;
r.size.width = r.size.width * scale;
CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, r);
UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect];
return sendImage;
}
}