苹果手机屏幕截图错误.<错误>:图像IO:CGImageDestinationFinalize Image Destin



编辑答案:

- (UIImage*) maskImage:(UIImageView *)maskImage withMask:(UIImageView *)cropImage
{
    UIImage *image = nil;
    UIImage *imagePNG = nil;
    CGSize newImageSize = CGSizeMake(cropImage.frame.size.width, cropImage.frame.size.height);
    UIGraphicsBeginImageContextWithOptions(newImageSize, NO, 0.0); //retina res
    [self.viewForImg.layer renderInContext:UIGraphicsGetCurrentContext()];
    image = UIGraphicsGetImageFromCurrentImageContext();

    NSData *imgData =  UIImagePNGRepresentation ( image ); // get PNG representation
    imagePNG = [UIImage imageWithData:imgData]; // wrap UIImage around PNG representation
    UIGraphicsEndImageContext(); 
    return imagePNG;
}

我试图截取我的iPhone屏幕,将图片保存到我的相机胶卷中。但遇到了这个错误。有人知道我的代码是否有问题吗?

May 22 14:13:34 unknown assetsd[281] <Error>: ImageIO: CGImageDestinationAddImage image parameter is nil
May 22 14:13:34 unknown assetsd[281] <Error>: ImageIO: CGImageDestinationFinalize image destination does not have enough images
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextScaleCTM: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextSetBaseCTM: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextSaveGState: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextSetInterpolationQuality: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextScaleCTM: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextSetFillColorWithColor: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextFillRects: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextScaleCTM: invalid context 0x0

法典:

- (IBAction)saveImage:(id)sender {
    self.imageOverlay.alpha = 1;
    self.savedImage = [self maskImage:self.imgView withMask:self.baseImgView];
    UIImageWriteToSavedPhotosAlbum(self.savedImage, self, @selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), nil);
}

- (UIImage*) maskImage:(UIImageView *)maskImage withMask:(UIImageView *)cropImage
{
    UIImage *image = nil;
    UIImage *imagePNG = nil;
    CGSize newImageSize = CGSizeMake(cropImage.frame.size.width, cropImage.frame.size.height);
    UIGraphicsBeginImageContextWithOptions(newImageSize, NO, 0.0); //retina res
    [self.viewForImg.layer renderInContext:UIGraphicsGetCurrentContext()];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext(); 
    NSData *imgData =  UIImagePNGRepresentation ( image ); // get PNG representation
    imagePNG = [UIImage imageWithData:imgData]; // wrap UIImage around PNG representation
    return imagePNG;
}

如果您要传递给UIGraphicsBeginImageContextWithOptions CGSize CGSizeZero,则可能会发生这种情况。我想cropImage参数要么是nil要么是它的大小是CGSizeZero.请调试并确保已初始化所有对象并使用正确的帧。

最新更新