我正在枚举iCloud驱动元素NSMetadataItem并将它们存储在数组中.
文件在那里:通过查看OS X上的iCloud文件夹以及使用UIDocumentPicker的设备进行确认.
稍后在代码中我尝试使用以下方法检索此类元素的缩略图:
// 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]; 
 我在处理程序中收到此错误:
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”}}
问题在于URL,你说,但URL是直接从`NSMetadataItem’中提取的,并且具有表单
file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~myCompany~myApp/Documents/0%20-%20intro.mov
…在这种情况下,电影被称为0 – intro.mov
我已经尝试了3种其他方法来下载缩略图.没有用.
突破
我现在已经发现我必须将iCloud驱动器上的所有文件(使用startDownloadingUbiquitousItemAtURL:error :)下载到设备才能生成缩略图,这似乎是疯了.想象一下,远程拥有几千兆字节的视频,并且必须下载所有文件才能显示缩略图.
它必须以另一种方式存在.证明是UIDocumentPicker显示iCloud上所有视频的缩略图而不下载它们.
有线索吗?
我认为问题是由于在找到URL之后读取文件的权限.但是,按照 this answer,您应该可以使用以下内容执行此操作:[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];
        
             