4

I am enumerating iCloud drive elements NSMetadataItem and storing them on an array.

The files are there: confirmed by looking at the iCloud folder on OS X and also on the device using UIDocumentPicker.

Later on the code I try to retrieve the thumbnails of such elements using:

// self.item is a NSMetadataItem
NSURL *fileURL = [self.item valueForAttribute:NSMetadataItemURLKey];

AVAsset *asset = [AVAsset assetWithURL:fileURL];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform = YES;

CMTime time = CMTimeMakeWithSeconds(1, 60);

AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
  if (result != AVAssetImageGeneratorSucceeded) {
    NSLog(@"couldn't generate thumbnail, error:%@", error);
  }
  
  // TODO Do something with the image
};

generator.maximumSize = size;
[generator generateCGImagesAsynchronouslyForTimes:@[[NSValue valueWithCMTime:time]]
                                completionHandler:handler];

I receive this error inside the handler:

couldn't generate thumbnail, error:Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo={NSLocalizedDescription=The requested URL was not found on this server., NSUnderlyingError=0x15f82b2a0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

the problem is with the URL, you say, but the URL is extracted directly from the `NSMetadataItem" and has the form

file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~myCompany~myApp/Documents/0%20-%20intro.mov

...in this case the movie is called 0 - intro.mov

I have tried 3 other methods to download the thumbnails. None works.

Breakthrough

I have discovered now that I have to download all files that are on iCloud drive (using startDownloadingUbiquitousItemAtURL:error:) to the device to be able to generate their thumbnails, what seems to be insane. Imagine having several gigabytes of videos remotely and having to download all files just to show their thumbnails.

It must exist another way. The proof is that the UIDocumentPicker shows the thumbnails of all videos that are on iCloud without downloading them.

Any clues?

pre
  • 3,475
  • 2
  • 28
  • 43
Duck
  • 34,902
  • 47
  • 248
  • 470
  • 1
    Does this help at all? http://stackoverflow.com/questions/27420380/nsurlthumbnaildictionarykey-empty-for-local-file – Charlie Aug 24 '15 at 01:00
  • especially for the lesser-known topics (and core anything), finding relevant documentation can be a nightmare. – Charlie Aug 24 '15 at 01:56

1 Answers1

9

I think the issue is arising due to permissions to read the files after the URL's have been found. Per this answer, however, you should be able to do it with the following:

[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];
Community
  • 1
  • 1
Charlie
  • 11,380
  • 19
  • 83
  • 138
  • for some reason, after upgrading to iOS 8.4.1, this is not working anymore – Duck Sep 10 '15 at 04:14
  • But... If the device has not yet downloaded the file at the given URL, `coordinateReadingItemAtURL` blocks while the file is downloaded. The file is downloaded first. – Marcin Dec 16 '16 at 01:01