【问题标题】:Can't create UIImage with [UIImage imageWithContentsOfFile:] and file is there无法使用 [UIImage imageWithContentsOfFile:] 创建 UIImage 并且文件在那里
【发布时间】:2014-12-28 04:34:48
【问题描述】:

我google了这个问题,大部分都用错了方法:[UIImage imageNamed:]。我不是。我确定该文件存在。以下代码在iOS8.1上运行。

        self.cachePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        UIImage *avatorImage = [[UIImage alloc] initWithCGImage:headCGImage];
        NSString *avatorName = [[[NSUUID alloc] init] UUIDString];
        avatorName = [avatorName stringByAppendingPathExtension:@"png"];
        avatorName = [self.cachePath stringByAppendingPathComponent:avatorName];
        NSData *avatorImageData = UIImagePNGRepresentation(avatorImage);
        BOOL writeSuccess = [avatorImageData writeToFile:avatorName atomically:YES];
        if (!writeSuccess)
           NSLog(@"Write to File Error");
        //read file form other place, it's just nil.
        UIImage *imageFromFile = [UIImage imageWithContentsOfFile:pathForImage];

然后我使用 [UIImage imageWithContentsOfFile:] 来获取 UIImage。有一件奇怪的事情。当我第一次运行应用程序时,一切正常。但是我停止了应用程序,然后再次运行它,[UIImage imageWithContentsOfFile:] 只返回一个零。我不知道为什么。

---------------我是可爱系-------------- ------------

Summery:从 iOS8 开始,每次运行应用时,来自 NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) 的目录不再相同。这就是为什么我的代码第一次运行就可以工作,之后就不行了。感谢 Midhun MP。

参考:Changes to App Containers in iOS8

【问题讨论】:

  • 你是如何找回图像的?
  • [UIImage imageWithContentsOfFile:]
  • 我从你的问题中得到的。我问你是如何传递文件名的,你是如何创建的
  • Em,实际上,我使用 Core Data 来存储我的数据。实体的属性是 NSString,我将路径字符串分配给该属性,然后在某个地方获取该实体,使用 someEntity.pathForBackup 获取图像的路径,然后使用 [UIImage imageWithContentsOfFile:],路径如下:/var/mobile /Containers/Data/Application/5A70DE99-6F63-4592-898D-15289BADE9DE/Documents/CF7DFDB8-0A5D-4C13-9DFE-1C6C96B59DDA.png
  • 您无法以这种方式获取图像。您不需要在核心数据中保留整个路径。而是保存文件名并在运行时计算路径。

标签: ios file uiimage


【解决方案1】:

从您的 cmets 中,您将整个路径保存在核心数据中并尝试使用该路径加载图像。

在最新的 iOS 版本中,每次关闭和打开应用时,36 个字符的文件夹都会发生变化。

所以不要保存整个路径,只保存文件名并使用以下方法检索它:

目标 C

NSString *yourImageName = @"CF7DFDB8-0A5D-4C13-9DFE-1C6C96B59DDA.png"; // Replace with actual image name
NSString *docDirPath    = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *pathForImage  = [docDirPath stringByAppendingPathComponent:yourImageName];
UIImage *imageFromFile  = [UIImage imageWithContentsOfFile:pathForImage];

斯威夫特:

var yourImageName = "CF7DFDB8-0A5D-4C13-9DFE-1C6C96B59DDA.png";
var docDir        = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
var docDirPath    = docDir[0] as? String
var pathForImage  = docDirPath!.stringByAppendingPathComponent(yourImageName)
var imageFromFile = UIImage(contentsOfFile: pathForImage)

您可以在这里找到详细信息Technical Note TN2406: Changes To App Containers In iOS 8

【讨论】:

  • 非常感谢。我按你说的改了代码,行得通!
  • @DeveloperYe:感谢您的评论。很高兴听到它对你有用。编码愉快。
  • 我在文档中搜索并没有找到关于这个的注释,你在哪里找到这个?
  • @DeveloperYe:实际上从 iOS 本身开始,当您更新应用程序时,位置名称就会改变。在 iOS 8 中,每次运行都会改变,您只需打印即可检查您的模拟器文件夹。
  • @DeveloperYe:我在这里找到它:developer.apple.com/library/ios/technotes/tn2406/_index.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-06
  • 1970-01-01
  • 1970-01-01
  • 2014-01-03
  • 1970-01-01
  • 2023-04-11
相关资源
最近更新 更多