【问题标题】:This used to work: Displaying image using imageWithContentsOfFile这曾经有效:使用 imageWithContentsOfFile 显示图像
【发布时间】:2014-08-19 16:04:26
【问题描述】:

昨天早上这对我有用,但现在不行了。所以,我怀疑是其他原因导致它发生了变化……但我找不到变化。我花了将近一周的时间来恢复我的代码,但它仍然没有工作(我知道昨天早上它还在工作)。所以,我希望在发布这个特定问题(症状?)时,我可以评估一些想法。谢谢。

我会根据需要下载图片:

NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *targetFile = [NSString stringWithFormat:@"%@/%@.%@", documentDirectory, imageName, imageType];

// only download those where an image exists
if(![imageType isEqualToString:@""])
{
    // only download the file if there is not already a local copy.
    if([filemgr fileExistsAtPath:targetFile] == NO)
    {
        NSMutableData *imageData = [[NSMutableData alloc] initWithLength:0];
        [imageData appendData:data];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *thumbNailFilename = [NSString stringWithFormat:@"%@.%@", imageName, imageType];
        NSString *thumbNailAppFile = [documentsDirectory stringByAppendingPathComponent:thumbNailFilename];
    }
}

然后显示它们:

NSString *imageFullName = [NSString stringWithFormat:@"%@%@", [greetingObject valueForKey:@"gid"], [greetingObject valueForKey:@"itp"]];
NSString *fullImagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:imageFullName];
UIImage *greetingImage = [UIImage imageWithContentsOfFile:fullImagePath];
self.greetingImage.image = greetingImage;

变量“imageFullName”和“fullImagePath”填充了正确的数据,并且图像文件存在于模拟器中的指定目录中。然而,“greetingImage”等于 nil。

这是我得到的“fullImagePath”:/Users/Steve2/Library/Application Support/iPhone Simulator/7.1/Applications/8C9F8417-F6E2-4B38-92B3-82A88477CB7F/Documents/165.jpg

以下是图片文件:

我还尝试了使用 initWithContentsOfFile 和 dataWithContentsOfFile 的变体并得到相同的结果。 greetingImage 变量为零。

我很欣赏你的想法。我什至重新安装了 Xcode,希望某些东西被破坏了。没有骰子。

添加:我刚刚想到的一件事...我昨天确实将 SystemConfiguration.framework 添加到项目中以获得不相关的功能。它目前位于链接框架和库列表的顶部。我没有使用这个的经验。会不会是导致问题的原因?

谢谢。

【问题讨论】:

  • 您是否通过在 Documents 目录中手动检查文件是否存在?
  • 是的。我可以在我的问题路径中的 Finder 中看到该文件。我将添加屏幕截图。
  • 您的图像很可能只是损坏了 - 没有正确下载/保存。要检查这一点,请尝试将 165.jpg 替换为从 Internet 手动下载的任何其他 jpg。
  • @Nekto - 我用可以在我的 Mac 上打开的图像替换了图像,应用程序也打开了它。但是,我知道存储图像的最佳方式是作为 NSData 对象。我使用此处的建议 (link) 下载并显示图像。这不是下载和存储图像的正确/最佳方式吗?谢谢。
  • @SteveSTL 问题应该是关于 SO 的另一个问题。我会接受 Joshua Button 的回答。

标签: ios objective-c


【解决方案1】:

您的代码看起来正确。

我会检查图像本身是否还可以。查看您发布的 Finder 屏幕截图并没有显示它应该使用有效 JPEG 进行的图像预览。您说图像正在下载,所以我怀疑它们在下载过程中以某种方式损坏。

编辑:

没有注意到您使用的是initWithContentsOfFile。由于您将文件保存为 NSData 对象,您需要将它们作为 NSData 对象加载到内存中,然后使用数据对象初始化 UIImage,如下所示:

 NSData *imageData = [NSData dataWithContentsOfFile:filePath];
 [UIImage imageWithData:imageData];

【讨论】:

  • 感谢@Joshua Button。请参阅我对 Nekto 的评论。
  • 如果上面显示的图像是写入磁盘的 NSData 对象,那么使用 +imageWithContentsOfFile: 将不起作用。你要找的是+imageWithData
  • JoshuaButton 和@Nekto 你是对的。 NSData 已损坏。服务器上发生错误并被附加到图像文件中。错误已修复,问题中的代码有效。谢谢。
猜你喜欢
  • 1970-01-01
  • 2017-08-19
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-11
相关资源
最近更新 更多