无法使用[UIImage imageWithContentsOfFile:]创建UIImage,并且文件在那里 [英] Can't create UIImage with [UIImage imageWithContentsOfFile:] and file is there

查看:360
本文介绍了无法使用[UIImage imageWithContentsOfFile:]创建UIImage,并且文件在那里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个问题有谷歌搜索,大多数使用错误的方法:[UIImage imageNamed:].我不是.并且我确定文件存在.以下代码在iOS8.1上运行.

I have google this question, most use a wrong method:[UIImage imageNamed:]. I am not. and I sure the file is existed. The follow code is run on 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:]仅返回nil.我不知道为什么.

then I use [UIImage imageWithContentsOfFile:] to get a UIImage. There is a strange thing. When I run app first time, all works. But I stop app, and run it again, [UIImage imageWithContentsOfFile:] return just a nil. I don't know why.

---------------我很可爱--------------- ------------

---------------------------I am a cute line---------------------------

摘要:从iOS8开始,每次运行应用程序时,NSSearchPathForDirectoriesInDomains中的目录(NSDocumentDirectory,NSUserDomainMask,YES)不再相同.这就是为什么我的代码在第一次运行时起作用,而在以后不起作用的原因.感谢Midhun议员.

Summery:begin in iOS8, directory from NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) is no longer be same every time you app run. That's why my code works first time run and not works after. Thanks for Midhun MP.

参考:对iOS8中应用容器的更改

推荐答案

根据您的评论,您正在将整个路径保存在核心数据中,并尝试使用该路径加载图像.

From your comments, you are saving the whole path in your core-data and trying to load the image using that path.

在最新的iOS版本中,每次关闭和打开应用程序时,都会更改36个字符的文件夹.

In latest iOS version that 36 character folders are changing each time when we close and open the app.

因此,不保存整个路径,而仅保存文件名并使用以下方法进行检索:

So instead of saving the whole path, only save the file name and retrieve it using:

目标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)

您可以在技术说明TN2406中找到详细信息: iOS 8中对应用容器的更改

You can find the details here Technical Note TN2406: Changes To App Containers In iOS 8

这篇关于无法使用[UIImage imageWithContentsOfFile:]创建UIImage,并且文件在那里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆