小编see*_*nte的帖子

无法使用[UIImage imageWithContentsOfFile:]创建UIImage,文件就在那里

我有谷歌这个问题,大多数使用错误的方法:[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];
Run Code Online (Sandbox Code Playgroud)

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

---------------------------我是一个可爱的线------------------ ---------

Summery:从iOS8开始,NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)的目录在每次应用程序运行时都不再相同.这就是为什么我的代码第一次运行而不能运行的原因.感谢Midhun MP.

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

file uiimage ios

2
推荐指数
1
解决办法
3143
查看次数

标签 统计

file ×1

ios ×1

uiimage ×1