<Error>:ImageIO:UIImagePNGRepresentation 中的 CGImageReadGetBytesAtOffset
<Error>: ImageIO: CGImageReadGetBytesAtOffset in UIImagePNGRepresentation
我能找到的关于此错误的唯一其他信息是 here,但没有帮助。
我尝试保存图像时出现以下错误。这似乎只在我同时拥有多张图片(~6)时才会发生。它何时发生似乎也是完全随机的。有时一切都很好,有时它会在 1 张图像上失败,有时是 3 张图像,有时应用程序会完全崩溃并出现 EXC_BAD_ACCESS
错误。
错误:ImageIO:CGImageReadGetBytesAtOffset:^^^ 错误 ^^^ CGImageSource 创建时数据大小:1144891 - 当前大小仅为:1003855
保存图片的代码如下:
- (void)saveWithImage:(UIImage *)anImage andFileName:(NSString *)aFileName {
NSString *subDirectory = @"Images";
NSString *fileName = [aFileName stringByAppendingString:@".png"];
NSString *documentsPath = [[CMAStorageManager sharedManager] documentsSubDirectory:subDirectory].path;
NSString *imagePath = [subDirectory stringByAppendingPathComponent:fileName];
__block NSString *path = [documentsPath stringByAppendingPathComponent:fileName];
__block NSData *data = UIImagePNGRepresentation(anImage);
self.image = anImage;
self.tableCellImage = anImage;
self.galleryCellImage = anImage;
self.imagePath = imagePath; // stored path has to be relative, not absolute (iOS8 changes UUID every run)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
if (![data writeToFile:path atomically:YES])
NSLog(@"Error saving image to path: %@", path);
dispatch_async(dispatch_get_main_queue(), ^{
});
});
}
我遇到了那个错误,结果我的图像没有保存(或者只保存了一半),这完全弄乱了 UI 显示和任何后续应用程序启动。我已将范围缩小到 UIImagePNGRepresentation
调用。
在相关说明中,该代码锁定了 UI;我想是因为 UIImagePNGRepresentation
电话;但是,据我所知 UIImagePNGRepresentation
不是线程安全的,所以我不能在后台执行它。有人知道解决这个问题的方法吗?
谢谢!
万一有人遇到类似问题,这就是为我解决的问题。
iPhone iOS saving data obtained from UIImageJPEGRepresentation() fails second time: ImageIO: CGImageRead_mapData 'open' failed
这是我用来在后台线程中保存 UIImage 的解决方案:
Convert UIImage to NSData without using UIImagePngrepresentation or UIImageJpegRepresentation
我收到此错误是因为我调用的 api 抛出 500 并在我试图转换为 PNG 的 tmp >> NSData 文件中返回 html 错误页面。
您可能希望在转换前检查您尝试打开的文件是否为图像。
如果您使用 dowloadTask 下载文件,请在移动 tmp > /Document/.png 之前检查 statusCode 是否为 200
heres my answer in other SO
我能找到的关于此错误的唯一其他信息是 here,但没有帮助。
我尝试保存图像时出现以下错误。这似乎只在我同时拥有多张图片(~6)时才会发生。它何时发生似乎也是完全随机的。有时一切都很好,有时它会在 1 张图像上失败,有时是 3 张图像,有时应用程序会完全崩溃并出现 EXC_BAD_ACCESS
错误。
错误:ImageIO:CGImageReadGetBytesAtOffset:^^^ 错误 ^^^ CGImageSource 创建时数据大小:1144891 - 当前大小仅为:1003855
保存图片的代码如下:
- (void)saveWithImage:(UIImage *)anImage andFileName:(NSString *)aFileName {
NSString *subDirectory = @"Images";
NSString *fileName = [aFileName stringByAppendingString:@".png"];
NSString *documentsPath = [[CMAStorageManager sharedManager] documentsSubDirectory:subDirectory].path;
NSString *imagePath = [subDirectory stringByAppendingPathComponent:fileName];
__block NSString *path = [documentsPath stringByAppendingPathComponent:fileName];
__block NSData *data = UIImagePNGRepresentation(anImage);
self.image = anImage;
self.tableCellImage = anImage;
self.galleryCellImage = anImage;
self.imagePath = imagePath; // stored path has to be relative, not absolute (iOS8 changes UUID every run)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
if (![data writeToFile:path atomically:YES])
NSLog(@"Error saving image to path: %@", path);
dispatch_async(dispatch_get_main_queue(), ^{
});
});
}
我遇到了那个错误,结果我的图像没有保存(或者只保存了一半),这完全弄乱了 UI 显示和任何后续应用程序启动。我已将范围缩小到 UIImagePNGRepresentation
调用。
在相关说明中,该代码锁定了 UI;我想是因为 UIImagePNGRepresentation
电话;但是,据我所知 UIImagePNGRepresentation
不是线程安全的,所以我不能在后台执行它。有人知道解决这个问题的方法吗?
谢谢!
万一有人遇到类似问题,这就是为我解决的问题。
iPhone iOS saving data obtained from UIImageJPEGRepresentation() fails second time: ImageIO: CGImageRead_mapData 'open' failed
这是我用来在后台线程中保存 UIImage 的解决方案:
Convert UIImage to NSData without using UIImagePngrepresentation or UIImageJpegRepresentation
我收到此错误是因为我调用的 api 抛出 500 并在我试图转换为 PNG 的 tmp >> NSData 文件中返回 html 错误页面。
您可能希望在转换前检查您尝试打开的文件是否为图像。
如果您使用 dowloadTask 下载文件,请在移动 tmp > /Document/.png 之前检查 statusCode 是否为 200
heres my answer in other SO