在 Objective-C 中将图像从项目资源复制到文档文件夹时出现问题
我在将图像从项目资源复制到文档文件夹时遇到问题。
我正在使用 NSFileManager 的 copyItemAtPath。当我在模拟器上测试它时,它工作正常,但当我在设备(iPod 或 iPad)上测试时,图像复制时出现错误。
使用 PhoneView 我可以看到复制的图像,但当我打开它时,图像是空的。当我在 Firefox 上打开它时,会显示下一条消息:
图像“file:///../logo.png”无法显示,因为它包含错误。
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *logoPath = [webPath stringByAppendingPathComponent:@"logo"];
if (![fileMgr fileExistsAtPath:logoPath]) {
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"logo" ofType:@"png"];
NSError *error = nil;
BOOL isSuccess = [fileMgr copyItemAtPath:resourcePath toPath:logoPath error:&
if (!isSuccess || error) {
if (error)
NSLog(@"ERROR copy logo: %@", [error description]);
}
}
有什么想法吗
?
提前致谢。
I'm having a problem copying an image from the project resources to the Documents folder.
I'm using NSFileManager's copyItemAtPath. When I test it on the simulator it works fine, but when I do it on the device (iPod or iPad) the image is copied with errors.
Using PhoneView I can see the copied image but when I open it, the image is empty. When I open it on Firefox the next message is shown:
The image “file:///../logo.png” cannot be displayed because it contains errors.
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *logoPath = [webPath stringByAppendingPathComponent:@"logo"];
if (![fileMgr fileExistsAtPath:logoPath]) {
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"logo" ofType:@"png"];
NSError *error = nil;
BOOL isSuccess = [fileMgr copyItemAtPath:resourcePath toPath:logoPath error:&
if (!isSuccess || error) {
if (error)
NSLog(@"ERROR copy logo: %@", [error description]);
}
}
}
Any ideas?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我在你的代码中找不到任何错误,我只能给出一个建议
将你的图像转换为 NSData
并写入文档文件夹
NSData *data;//来自您徽标图像的数据
i cant find any error in your code , i can only give one suggestion
convert your image into NSData
and write to document folder
NSData *data;//data from you logo image
我不完全确定,但我认为你的台词:
应该是:
我认为 iOs 不会将“logo”识别为“logo.png”。
I'm not completely sure but I think your line:
should be:
I think iOs does not recognize "logo" as "logo.png".
看一下文件名中的大写字符,有时会出现错误,iPhone 区分大小写,mac-simulator 可能不区分大小写...
take a look at upperCase character in file name, sometimes errors come from that, iPhone is case sensitives, mac-simulator probably is not...
首先,我要感谢大家的回答,他们都帮助解决了我的问题。
我不知道为什么,但是当我复制 PNG 图像时,图像不知怎的被损坏了,当我尝试对 JPG 图像做同样的事情时,问题就解决了。
谢谢!。
First of all, I would like to thank you all for your answers, all of them helped to resolve my issue.
I'm not sure why, but when I copy a PNG image, somehow the image was corrupted, when I tried doing the same thing with a JPG image the problem was solved.
Thanks!.
原来是文件格式的问题。当我将图像更改为 JPG 时,问题就消失了。苹果似乎改变了 PNG 图像,我对此不太确定,但就我的经历而言,这是有道理的。
谢谢!
It turned out to be a problem with the file format. When I changed the image to a JPG, the problem was gone. It seems that apple changes PNG images, I'm not really sure about this, but for what I've experienced, it makes sence.
Thanks!
@mikywan 正确地指出 JPG 工作完美,但从资源包复制时的 PNG 图像已损坏。
如果有人仍然想使用 png 图像,这里是一个解决方案,请选择目标上的构建设置并将“压缩 PNG 文件”设置为“否”。构建应用程序时,资源内的 PNG 不会更改。
@mikywan has correctly pointed out that the JPG works perfect but the PNG images when copied from the resource bundle are corrupted.
If someone still wants to use the png images here is a solution, select the build settings on your target and set the "Compress PNG Files" to "NO". The the PNGs inside the resources are not will not be changed while building the app.
古老的问题,但看起来苹果仍然很糟糕,所以两年后问题仍然存在。万一其他人遇到这个问题,这就是我解决它的方法,这基本上阐明了 @Rajesh 已经说过的内容:
Ancient question, but it looks like Apple still suck so the problem is still there 2 years later. In case someone else comes across the problem, this is how I solved it, which is basically spelling out what @Rajesh already said: