iOS Core Data无法从存储的URI打开图像 [英] iOS Core Data can't open image from stored URI

查看:116
本文介绍了iOS Core Data无法从存储的URI打开图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我将图片存储在我的文档文件夹中,并将其URI存储在数据库中。

In my app i store images in my documents folder and store their URI in the database.

每当我尝试根据存储的URI加载图片在db中,我得到nil作为结果。

Whenever i'm trying to load the image based on the URI stored in the db, i get "nil" back as a result.

存储的URI如下所示:

The stored URI looks like this:


/ Users / myname / Library / Application Support / iPhone
Simulator / 6.0 / Applications / 78325CF9-73B0-4D0D-B136-FD8FF8D837C1 / Documents / WC_45FEAF25-AC0C-4E91-A5E2-AA48215F1E48-3147-000005C6C9E65B42.jpg

/Users/myname/Library/Application Support/iPhone Simulator/6.0/Applications/78325CF9-73B0-4D0D-B136-FD8FF8D837C1/Documents/WC_45FEAF25-AC0C-4E91-A5E2-AA48215F1E48-3147-000005C6C9E65B42.jpg

当我看到它从iTerm我看到的图像是在那里,但在iTerm我必须cd进入目录像这样:

When i look into it from iTerm i see that the image is there, but in iTerm i have to cd into the directory like this:


/ Users / myname / Library / Application\ Support / iPhone\
Simulator / 6.0 / Applications / 78325CF9-73B0-4D0D-B136-FD8FF8D837C1 /

/Users/myname/Library/Application\ Support/iPhone\ Simulator/6.0/Applications/78325CF9-73B0-4D0D-B136-FD8FF8D837C1/Documents/WC_45FEAF25-AC0C-4E91-A5E2-AA48215F1E48-3147-000005C6C9E65B42.jpg

注意在空格之前的反斜杠

notice the backslashes before whitespaces

我想使用此功能获取图片:

I'm trying to get the image using this function:

-(UIImage *) getImageFromURL:(NSString *)fileURL {
    UIImage * result;

    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
    result = [UIImage imageWithData:data];

    return result; //result is always nil
}

问题与反斜杠有关吗?
任何帮助都非常感谢!

Is the problem related to the backslashes? Any help is very much appreciated!

此致

Zoli

推荐答案

解决方案是不将uri存储在数据库中,只存储图像名称。 (但仍然在文档目录中存储图像)
然后使用以下函数可以检索图像:

The solution was to not store the uri in the database, only the image name. (But still store the image in the documents directory) Then with the following function the image can be retrieved:

- (UIImage*)loadImageWithName:(NSString*) name
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:
                      [NSString stringWithString: name] ];
    UIImage* image = [UIImage imageWithContentsOfFile:path];
    return image;
}

这篇关于iOS Core Data无法从存储的URI打开图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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