3

我正在尝试完成一项简单的任务,例如将 a 转换UIImageNSData,因为AVPlayerItem当我从PHImageManager. 什么可能相当于UIImagePNGRepresentation在数据中转换视频:

PHVideoRequestOptions *videoRequestOptions = [[PHVideoRequestOptions alloc] init];
videoRequestOptions.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
videoRequestOptions.version = PHVideoRequestOptionsVersionOriginal;

[[PHImageManager defaultManager] requestPlayerItemForVideo:asset options:videoRequestOptions resultHandler:^(AVPlayerItem *item, NSDictionary *info)
            {
                //?
            }];

UIImage事情是这样的:

[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFit options:imageRequestOptions resultHandler:^(UIImage *result, NSDictionary *info)
{
     NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(result)]; //<==THIS
}
4

1 回答 1

2

解决方案是从 AVPlayerItem 资产中获取 URL,然后从该 URL 创建 NSData:

PHAsset *asset = ...
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
[[PHImageManager defaultManager] requestPlayerItemForVideo:asset options:options resultHandler:^(AVPlayerItem * _Nullable playerItem, NSDictionary * _Nullable info) {
                NSURL *fileURL = [(AVURLAsset *)playerItem.asset URL];
                NSData *videoData = [NSData dataWithContentsOfURL:fileURL];
                NSLog(@"tmpData Size: %lu",tmpData.length);
}];

另一种方法是使用 'requestAVAssetForVideo:asset' ....

PHFetchOptions *fetchOption = [[PHFetchOptions alloc]init];
if ([fetchOption respondsToSelector:@selector(setFetchLimit:)]) {
    [fetchOption setFetchLimit:1];
}
PHAsset *asset = [PHAsset fetchAssetsWithLocalIdentifiers:@[@"<Video-localIdentifier>"] options:fetchOption].firstObject;
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc]init];
options.version = PHVideoRequestOptionsVersionOriginal;
options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;

[[PHImageManager defaultManager] requestAVAssetForVideo:asset
                                                options:options
                                          resultHandler:
 ^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
     AVURLAsset *urlAsset = (AVURLAsset *)asset;
     NSData *videoData = [NSData dataWithContentsOfURL:urlAsset.URL];

     if (videoData) {
         NSLog(@"videoData Size: %lu", videoData.length);
     }

 }];
于 2016-09-06T16:11:57.273 回答