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

查看:27
本文介绍了本地文件的 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.

以下是这种情况下的 URL 示例:

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天全站免登陆