简体   繁体   中英

<Error>: ImageIO: CGImageReadGetBytesAtOffset in UIImagePNGRepresentation

The only other information I could find on this error was here , which wasn't helpful.

I get the following error when I try to save images. This seems to only happen when I have several images (~6) at once. It also seems to be completely random as to when it occurs. Sometimes everything is fine, sometimes it'll fail on 1 image, sometimes 3, and sometimes the app will completely crash in a EXC_BAD_ACCESS error.

Error: ImageIO: CGImageReadGetBytesAtOffset : ^^^ ERROR ^^^ CGImageSource was created with data size: 1144891 - current size is only: 1003855

Here is the code that saves the image:

- (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(), ^{

        });
    });
}

I get that error and as a result my images aren't saved (or only half of them are saved), which completely messes up the UI display, and any subsequent app launches. I've narrowed it down to the UIImagePNGRepresentation call.

On a related note, that code locks up the UI; I think because of the UIImagePNGRepresentation call; however, as far as I know UIImagePNGRepresentation is no thread safe, so I can't do it in the background. Does anyone know a way around this?

Thanks!

In case anyone comes across a similar issue, this is what fixed it for me.

iPhone iOS saving data obtained from UIImageJPEGRepresentation() fails second time: ImageIO: CGImageRead_mapData 'open' failed

and this is the solution I used to save UIImages in a background thread:

Convert UIImage to NSData without using UIImagePngrepresentation or UIImageJpegRepresentation

I was getting this error because the api I called was throwing a 500 and returning html error page in the tmp >> NSData file I was trying to convert to PNG.

You may wish to check the file your trying to open is a image at all before conversion.

If youre downloading the file with dowloadTask then check statusCode is 200 before moving tmp > /Document/.png

heres my answer in other SO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM