2

编辑答案:

- (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;
}
4

1 回答 1

2

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

于 2012-05-22T07:06:27.820 回答