0
- (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

なんで?

4

1 に答える 1