我为此编写的这种方法非常有效:
+ (UIImage*) getTheArea:(CGRect)area inView:(UIView*)view{
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
UIGraphicsBeginImageContextWithOptions(CGSizeMake(area.size.width, area.size.height), NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(view.bounds.size);
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(c, -area.origin.x, -area.origin.y); // <-- shift everything up by 40px when drawing.
[view.layer renderInContext:c];
UIImage* thePrintScreen = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return thePrintScreen;
}
例如,如果您想制作主视图的打印屏幕,在 (100,50,100,100)
UIImage* image = [self getTheArea:CGRectMake(100,50,100,100) inView:view];