11

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.

viperacr99
  • 423
  • 2
  • 5
  • 10
  • Just out of interest: have you tried this with the compression value set to something *other* than 1.0 on both those calls (eg, 0.5?). – lxt Feb 22 '11 at 17:05
  • The image is corrupt. The JPEG image should end with 0xFF 0xD9. How are you constructing `myImage`? – Dylan Copeland Feb 22 '11 at 17:13
  • Can you link here the jpeg generated by your app, so we can take a look? – Please treat your mods well. Feb 22 '11 at 18:22
  • I have not tried with a compression value other than 1.0. – viperacr99 Feb 22 '11 at 22:41
  • I'm taking a picture with the camera or selecting one from the camera roll (using standard image picker), then resizing it to a thumbnail. The image is constructed by the imageWithContentsOfFile selector of UIImage, giving it the path to the thumbnail. I don't understand how I can read the file into a UIImage, display it successfully, but then it is corrupt when passing that image to UIImageJPEGRepresentation(). I'm not doing anything else with that UIImage except holding on to it from the time the user opens the tableview to the time my save button is pressed. – viperacr99 Feb 22 '11 at 22:48
  • 1
    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. – Pieter Witvoet Apr 21 '11 at 07:06
  • @PieterWitvoet I think you are right, I tried the same thing on `NSURLConnection` and I printed the image data through `NSLog` and I found as you said that the image did start with 0xFFD8 and end with 0xFFD9. I saw a [post](http://stackoverflow.com/questions/8173382/ipad-imageio-error-jpegnot-a-jpeg-file-starts-with-0xff-0xd9) where they faced the same problem on iOS 4.3 but did not face it on iOS 5.0, so it could be an incorrect message. – antf Aug 17 '12 at 14:31

1 Answers1

21

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];
viperacr99
  • 423
  • 2
  • 5
  • 10