NSURLThumbnailDictionaryKey为空的本地文件 [英] NSURLThumbnailDictionaryKey empty for local file

查看:517
本文介绍了NSURLThumbnailDictionaryKey为空的本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取一个文件的缩略图,我必须在我的应用程序中显示。我在这里使用NSURL:

I want to get a thumbnail representation of a file I have to display in my app. I'm using NSURL here:

NSDictionary *thumbnails = nil;
BOOL success = [fileURL getResourceValue:&thumbnails
                                          forKey:NSURLThumbnailDictionaryKey
                                           error: &error];

如果我连接到iCloud,这个工作正常,并且URL是指向存储在iCloud。 fileURL是这样的:

This works fine if I am connected to iCloud, and the URL is a link to a file stored in iCloud. The fileURL is something like:

file:///Users/me/Library/Mobile%20Documents/BJXXGLR9R3~com~myapp~icloud/FileStorage/contact-page%20copy.png

如果我使用与NSURL相同的代码指向本地文件,但缩略图字典为空。

If I use the same code with a NSURL pointing to a local file, however, the thumbnails dictionary is empty.

以下是此案例中网址的示例:

Here is an example of the URL in this case:

file:///Users/me/Library/Containers/com.mycompany.mymacapp/Data/Library/Application%20Support/com.mycompany.mymacapp/FileStorage/Bn4VaCnCUAEJjLb.png-large.png

此getResourceValue的API不应该与本地存储的文件一起使用吗?或者我做错了什么?

Is this API for getResourceValue not supposed to work with locally stored files? Or am I doing something wrong?

推荐答案

这是API的一部分。此外,您应该使用文件协调器,同时处理来自iCloud的文件:

This is part of the API. Furthermore you should use a File coordinator while working with files coming from iCloud:

    [url startAccessingSecurityScopedResource];

NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] init];
__block NSError *error;
[coordinator coordinateReadingItemAtURL:url options:0 error:&error byAccessor:^(NSURL *newURL) {
    [newURL getResourceValue:&image forKey:NSURLThumbnailDictionaryKey error:&error];
}];

[url stopAccessingSecurityScopedResource];

这篇关于NSURLThumbnailDictionaryKey为空的本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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