我是否必须下载所有 iCloud 文件才能生成它们的缩略图?
Do I have to download all iCloud files to be able to generate their thumbnails?
我正在枚举 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种方法来下载缩略图。 None 有效。
突破
我现在发现我必须将 iCloud 驱动器上的所有文件(使用 startDownloadingUbiquitousItemAtURL:error:
)下载到设备才能生成它们的缩略图,这似乎很疯狂。想象一下,远程拥有数 GB 的视频并且必须下载所有文件才能显示它们的缩略图。
它必须以另一种方式存在。证明是 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];
我正在枚举 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种方法来下载缩略图。 None 有效。
突破
我现在发现我必须将 iCloud 驱动器上的所有文件(使用 startDownloadingUbiquitousItemAtURL:error:
)下载到设备才能生成它们的缩略图,这似乎很疯狂。想象一下,远程拥有数 GB 的视频并且必须下载所有文件才能显示它们的缩略图。
它必须以另一种方式存在。证明是 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];