简体   繁体   中英

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.

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.

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