我的Application Resource Bundle中有许多图像(PNG,GIF和JPG)。 我想在我的文档目录中保存一些图像,所以我使用:
imgPath=[documentsDirectoryPath stringByAppendingPathComponent:@"myImage.png"];
if (![fileMgr fileExistsAtPath:imgPath]) {
[[fileMgr contentsAtPath:[[NSBundle mainBundle] pathForResource:@"myImage"ofType:@"png"]] writeToFile:imgPath atomically:NO];
}
这会将Image文件保存在我想要的Path上,但是这个文件中有300个字节(可能是垃圾数据),导致图像损坏... 我在这里做错了吗?
这适用于模拟器,但在真实设备上,图像有一些额外的300字节。 此外,GIF图像可以很好地复制并且可以正常工作,但PNG图像会出现此问题。
答案 0 :(得分:1)
您可以尝试使用NSFileManagers
-(BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error
方法查看效果是否更好。
如果您尝试在Mac上查看已保存的PNG,则可能会遇到问题,因为Xcode会在构建您的应用时将您的PNG转换为iPhone优化版本。
Here is more info on the optimizations performed,此处Apple's Technical Q&A用于撤消优化,以便您可以在桌面上查看图片。