NSURLThumbnailDictionaryKey empty for local file

Deadly 提交于 2019-12-22 08:44:43

问题


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];

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

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

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


回答1:


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];


来源:https://stackoverflow.com/questions/27420380/nsurlthumbnaildictionarykey-empty-for-local-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!