NSURLThumbnailDictionaryKey empty for local file

I want to get a thumbnail view of the file that should be displayed in my application. I am using NSURL here:

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

This works great if I'm connected to iCloud, and the URL is a link to a file stored in iCloud. The url file looks something like this:

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

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

Here is an example URL in this case:

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

This API for getResourceValue should not work with locally stored files? Or am I doing something wrong?

+4
source share
1 answer

API. , 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];
+3

All Articles