iosicloudicloud-drive

Do I have to download all iCloud files to be able to generate their thumbnails?


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?


Solution

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