- (UIImage*) screenshot
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0.0);
//UIGraphicsBeginImageContext(self.bounds.size);
PO (NSStringFromCGRect(self.bounds));
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return myImage;
}
它适用于我的所有UIView。但是,如果我在这样的代码中生成我自己的UIView
+(UIImage *) imageAsScreenShotFromLabel:(NSString*) strLabel
{
UILabel * lbl=[[UILabel alloc]init];
lbl.text =strLabel;
[lbl resizeToStretch];
return lbl.screenshot;
}
失败了,我收到了这个错误:
Sep 12 15:41:05 Jupiter.local [70630] <Error>: CGContextSaveGState: invalid context 0x0
Sep 12 15:41:05 Jupiter.local [70630] <Error>: CGContextClipToRect: invalid context 0x0
Sep 12 15:41:05 Jupiter.local [70630] <Error>: CGContextSetAlpha: invalid context 0x0
Sep 12 15:41:05 Jupiter.local [70630] <Error>: CGContextRestoreGState: invalid context 0x0
为什么?
答案 0 :(得分:-1)