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

柔情痞子 提交于 2019-12-09 04:25:45

问题


I have read all answers on stack overflow about this error and none solved the case, because my question is a little different.

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?


回答1:


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


来源:https://stackoverflow.com/questions/32157625/do-i-have-to-download-all-icloud-files-to-be-able-to-generate-their-thumbnails

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