在 Objective-C 中将图像从项目资源复制到文档文件夹时出现问题

发布于 2024-10-20 11:04:01 字数 814 浏览 1 评论 0原文

我在将图像从项目资源复制到文档文件夹时遇到问题。

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

非要怀念 2024-10-27 11:04:01

我在你的代码中找不到任何错误,我只能给出一个建议

将你的图像转换为 NSData
并写入文档文件夹

NSData *data;//来自您徽标图像的数据

[data writeToFile:<document path> atomically:YES];

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

[data writeToFile:<document path> atomically:YES];
小兔几 2024-10-27 11:04:01

我不完全确定,但我认为你的台词:

NSString *logoPath = [webPath stringByAppendingPathComponent:@"logo"];

应该是:

NSString *logoPath = [webPath stringByAppendingPathComponent:@"logo.png"];

我认为 iOs 不会将“logo”识别为“logo.png”。

I'm not completely sure but I think your line:

NSString *logoPath = [webPath stringByAppendingPathComponent:@"logo"];

should be:

NSString *logoPath = [webPath stringByAppendingPathComponent:@"logo.png"];

I think iOs does not recognize "logo" as "logo.png".

任性一次 2024-10-27 11:04:01

看一下文件名中的大写字符,有时会出现错误,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...

对你而言 2024-10-27 11:04:01

首先,我要感谢大家的回答,他们都帮助解决了我的问题。

我不知道为什么,但是当我复制 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!.

明月松间行 2024-10-27 11:04:01

原来是文件格式的问题。当我将图像更改为 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!

夜灵血窟げ 2024-10-27 11:04:01

@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.

葮薆情 2024-10-27 11:04:01

古老的问题,但看起来苹果仍然很糟糕,所以两年后问题仍然存在。万一其他人遇到这个问题,这就是我解决它的方法,这基本上阐明了 @Rajesh 已经说过的内容:

BOOL success = [UIImagePNGRepresentation([UIImage imageNamed:@"icon"]) 
                   writeToFile:logoPath atomically:YES];

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:

BOOL success = [UIImagePNGRepresentation([UIImage imageNamed:@"icon"]) 
                   writeToFile:logoPath atomically:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文