iOS UIImageJPEGRepresentation error: Not a JPEG file: starts with 0xff 0xd9

11,428

The images were not corrupt, I was able to display them correctly. The issue is possibly a bug in UIImage, or perhaps the documentation should be more clear about using imageWithContentsOfFile:.

I was able to eliminate the error message by changing

UIImage *myImage = [UIImage imageWithContentsOfFile:path];

to

NSData *img = [NSData dataWithContentsOfFile:path];
UIImage *photo = [UIImage imageWithData:img];
Share:
11,428

Related videos on Youtube

Author by

viperacr99

Updated on April 26, 2020

Comments

  • viperacr99 over 2 years

    I am writing a .jpg file to my app's Documents directory like this:

    NSData *img = UIImageJPEGRepresentation(myUIImage, 1.0);
    BOOL retValue = [img writeToFile:myFilePath atomically:YES];
    

    Later, I load that image back into a UIImage using:

    UIImage *myImage = [UIImage imageWithContentsOfFile:path];
    

    I know it works because I can draw the image in a table cell and it is fine. Now if I try to use UIImageJPEGRepresentation(myImage, 1.0), the debugger prints out these lines:

    <Error>: Not a JPEG file: starts with 0xff 0xd9
    <Error>: Application transferred too few scanlines
    

    And the function returns nil. Does anybody have an idea why this would happen? I haven't done anything to manipulate the UIImage data after it was loaded. I just provided the UIImage to an image view in a cell. I set the image view properties such that all the images in the cells line up and are the same size, but I don't think that should have anything to do with being able to convert the UIImage to NSData.

    • Pieter Witvoet
      Pieter Witvoet
      I recently came across this same error message when trying to load images through a NSURLConnection. You'd expect the images to be corrupt, but they actually did start with 0xFFD8 and end with 0xFFD9 (and display fine in a browser), as a JPEG file should... so at the very least this error message is incorrect.
  • SuperGuyAbe about 8 years
    Thanks for confirming my suspicions that this might be about the difference between data and file.

Related