2015-08-22 25 views
2

我已閱讀堆棧溢出有關此錯誤的所有答案,並沒有解決這種情況,因爲我的問題有點不同。我是否必須下載所有iCloud文件才能生成縮略圖?

我正在枚舉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

... in thi這部電影被稱爲0 - intro.mov

我已經嘗試了3種其他方法來下載縮略圖。沒有用。

突破

我現在,我要下載的iCloud的驅動器上的所有文件發現(使用startDownloadingUbiquitousItemAtURL:error:)到設備,能夠產生自己的縮略圖,似乎是瘋了。想象一下,遠程擁有幾千兆字節的視頻,並且必須下載所有文件才能顯示縮略圖。

它必須以另一種方式存在。證明是,UIDocumentPicker顯示了iCloud上所有未下載視頻的縮略圖。

任何線索?

+1

這有幫助嗎? http://stackoverflow.com/questions/27420380/nsurlthumbnaildictionarykey-empty-for-local-file – Charlie

+0

特別是對於鮮爲人知的話題(以及任何核心內容),查找相關文檔可能是一場噩夢。 – Charlie

回答

3

我認爲這個問題是由於在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]; 
+0

不能投你夠!我打了3天這個!謝謝!!!!! – SpaceDog

+0

@SpaceDog當然是:-) – Charlie

+0

由於某種原因,在升級到iOS 8.4.1之後,這不再起作用 – SpaceDog

相關問題