jAmi
jAmi

Reputation: 779

While saving a PNG image using NSData writetofile saves corrupted data on the iphone disk

I have a number of images (PNG,GIF and JPG) in my Application Resource Bundle. I want some images to be saved in my Documents Directory so i use :

    imgPath=[documentsDirectoryPath stringByAppendingPathComponent:@"myImage.png"];

    if (![fileMgr fileExistsAtPath:imgPath]) {

    [[fileMgr contentsAtPath:[[NSBundle mainBundle] pathForResource:@"myImage"ofType:@"png"]] writeToFile:imgPath atomically:NO];

}

This saves an Image file on my desired Path but this file has an Extra 300 bytes (of maybe junk data) in it which results in a corrupted image... Am i doing something wrong here?

This works in the simulator but on the real device the image has some extra 300 bytes. Also a GIF image gets copied nicely and works but this problem occurs for PNG image.

Upvotes: 0

Views: 1053

Answers (1)

Martin Gordon
Martin Gordon

Reputation: 36389

You could try using NSFileManagers -(BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error method to see if you get better results.

If you're trying to view the saved PNG on your Mac, you may have issues since Xcode will convert your PNGs to iPhone optimized versions when it's building your app.

Here is more info on the optimizations performed, and here is Apple's Technical Q&A for undoing the optimizations so you can view the image on your desktop.

Upvotes: 1

Related Questions