4

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?

Z S
  • 7,039
  • 12
  • 53
  • 105

1 Answers1

4

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];
cescofry
  • 3,706
  • 3
  • 26
  • 22
  • Thanks. Is it documented anywhere that this is how the API is supposed to behave (i.e. getResourceValue only works for files in iCloud)? I looked around but couldn't get any confirmation. – Z S Dec 15 '14 at 19:23