如何在iCloud Drive上生成图像的缩略图?

Spa*_*Dog 6 thumbnails ios photosframework phasset icloud-drive

这似乎很容易,但缺乏文档使得这个问题无法猜测.

我在我的应用程序的icloud驱动器上有图片和视频,我想创建这些资产的缩略图.我说的是iCloud Drive上的资产,而不是相机胶卷内的iCloud照片流.我在谈论真正的iCloud Drive文件夹.

与图像相比,从视频创建缩略图"很容易".您只需要2周的时间来弄清楚它是如何工作的,记住Apple写的文档很差,但是图像的缩略图似乎是不可能的.

我现在拥有的是一个数组,NSMetadataItem每个数组描述iCloud文件夹中的一个项目.

这些是到目前为止我尝试过的方法不起作用:


方法1

[fileURL startAccessingSecurityScopedResource];

NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] init];
__block NSError *error;

[coordinator coordinateReadingItemAtURL:fileURL
                                options:NSFileCoordinatorReadingImmediatelyAvailableMetadataOnly
                                  error:&error
                             byAccessor:^(NSURL *newURL) {
                               NSDictionary *thumb;
                               BOOL success = [newURL getResourceValue:&thumb forKey:NSURLThumbnailDictionaryKey error:&error];

                               UIImage *thumbnail = thumb[NSThumbnail1024x1024SizeKey];



                             }];

[fileURL stopAccessingSecurityScopedResource];
Run Code Online (Sandbox Code Playgroud)

这种方法的结果太棒了.准备好了吗?在这里,我们去:success = YES,error = nil缩略图=零.


另一种方法

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:fileURL
                                            options:nil];

AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];

imageGenerator.appliesPreferredTrackTransform = YES;

CMTime time = CMTimeMake(0, 60); // time range in which you want
NSValue *timeValue = [NSValue valueWithCMTime:time];

[imageGenerator generateCGImagesAsynchronouslyForTimes:@[timeValue] completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * error) {

  thumbnail = [[UIImage alloc] initWithCGImage:image];

}];
Run Code Online (Sandbox Code Playgroud)

error = The requested URL was not found on this server.thumbnail = nil

此方法似乎仅适用于视频.我试着这样以防万一.图像的任何等效方法?


原始方法

NSData *tempData = [NSData dataWithContentsOfUrl:tempURL];
Run Code Online (Sandbox Code Playgroud)

NOPE - 数据=零


方法4

第四种可能的方法是使用,ALAsset但这在iOS 9上已被弃用.

我认为所有这些方法都失败了,因为如果资源是本地的,它们只是工作(bug或不工作).有关如何下载图像以便获取缩略图的任何想法?

还有其他想法吗?

谢谢


编辑:经过多次测试后,我发现方法1是唯一一个看似正确的方向.这种方法效果不佳,有时会抓取图标,但大部分时间都不起作用.


另一点是这个.无论人们建议我什么,他们总是说要下载整个图像来获取缩略图.我不认为这是要走的路.只需看看如何获​​取视频的缩略图.您不下载整个视频以获取其缩略图.

所以这个问题仍然存在.

Spa*_*Dog 2

在测试了几种解决方案后,似乎效果更好的一种是以下一种:

[fileURL startAccessingSecurityScopedResource];

NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] init];
__block NSError *error;

[coordinator coordinateReadingItemAtURL:fileURL
                                options:NSFileCoordinatorReadingImmediatelyAvailableMetadataOnly
                                  error:&error
                             byAccessor:^(NSURL *newURL) {
                               NSDictionary *thumb;
                               BOOL success = [newURL getResourceValue:&thumb forKey:NSURLThumbnailDictionaryKey error:&error];

                               UIImage *thumbnail = thumb[NSThumbnail1024x1024SizeKey];



                             }];

[fileURL stopAccessingSecurityScopedResource];
Run Code Online (Sandbox Code Playgroud)

这个解决方案并不完美。有时它无法显示缩略图,但我找不到任何其他 100% 有效的解决方案。其他的情况比这更糟糕。