某些 iPhone 4s 和 iPod Touch 上的随机 img 保存错误
Posted
技术标签:
【中文标题】某些 iPhone 4s 和 iPod Touch 上的随机 img 保存错误【英文标题】:Random img saving error on some iPhone 4s and iPod Touches 【发布时间】:2012-05-21 09:11:05 【问题描述】:我遇到了一个奇怪的错误。我希望该应用程序将图像保存到相机胶卷中。
但少量设备给我保存消息时出错。
编辑:错误日志
“未能为保存的照片编码图像。” UserInfo=0x3e7170 NSUnderlyingError=0x389310 "无法为保存的照片编码图像。", NSLocalizedDescription=无法为保存的照片编码图像。
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
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextScaleCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextSetBaseCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextSaveGState: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextSetInterpolationQuality: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextScaleCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextSetFillColorWithColor: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextFillRects: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextScaleCTM: 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>: CGContextTranslateCTM: 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>: CGContextConcatCTM: 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>: CGContextDrawImage: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextConcatCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextTranslateCTM: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextDrawImage: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGContextRestoreGState: invalid context 0x0
May 22 14:13:34 unknown assetsd[281] <Error>: CGBitmapContextCreateImage: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGContextRestoreGState: invalid context 0x0
May 22 14:13:34 unknown com.apple.assetsd[281] <Notice>: May 22 14:13:34 SLQ-iTouch assetsd[281] <Error>: CGBitmapContextCreateImage: invalid context 0x0
>
下面是我用来保存图片的代码。
- (IBAction)saveImage:(id)sender
HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.dimBackground = YES;
HUD.labelText = @"Its saving!";
self.imageOverlay.alpha = 1;
self.savedImage = [self maskImage:self.imgView withMask:self.baseImgView];
UIImageWriteToSavedPhotosAlbum(self.savedImage, self, @selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), nil);
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
NSString *message;
NSString *title;
if (!error)
title = NSLocalizedString(@"Save Success", @"");
message = NSLocalizedString(@"Save Success Message", @"");
HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Checkmark.png"]];
HUD.mode = MBProgressHUDModeCustomView;
HUD.labelText = @"We’re saved - WooHoo!!";
[HUD hide:YES afterDelay:1.5];
saved =1;
self.imageOverlay.alpha =0.85;
[self performSelector:@selector(saveSuccess) withObject:nil afterDelay:1.5];
else
title = NSLocalizedString(@"Save Failed", @"");
message = [error description];
HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sad_face.png"]];
HUD.mode = MBProgressHUDModeCustomView;
HUD.labelText = @"Error! Try saving photo again :p";
[HUD hide:YES afterDelay:3];
- (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;
【问题讨论】:
保存时会得到什么? 能否将错误信息添加到您的问题中? 您好,感谢您的回复。我设法创建了日志,因为是随机的 【参考方案1】:- (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 4s 和 iPod Touch 上的随机 img 保存错误的主要内容,如果未能解决你的问题,请参考以下文章
如何通过代码查找iPhone / iPod设备型号(3G,3GS,4,4S)? [重复]
iPod Touch 和 iPhone 上的核心位置有啥区别?
检测特定的iPhone / iPod touch型号[重复]