我想弄清楚,但找不到任何有用的信息.
我才发现这个:
我才发现这个:
PHAssetResourceManager.defaultManager().writeDataForAssetResource(assetRes,toFile: fileURL,options: nil,completionHandler: { // Video file has been written to path specified via fileURL }
但我很惭愧地说我不知道怎么玩它.
我创建了一个UIImagePickerController,并从相机胶卷中加载了一个Image.
解决方法
使用此代码从实时照片中获取视频:
- (void)videoUrlForLivePhotoAsset:(PHAsset*)asset withCompletionBlock:(void (^)(NSURL* url))completionBlock{ if([asset isKindOfClass:[PHAsset class]]){ NSString* identifier = [(PHAsset*)asset localIdentifier]; NSString* filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mov",[NSString stringWithFormat:@"%.0f",[[NSDate date] timeIntervalSince1970]]]]; NSURL *fileUrl = [NSURL fileURLWithPath:filePath]; PHLivePhotoRequestOptions* options = [PHLivePhotoRequestOptions new]; options.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat; options.networkAccessAllowed = YES; [[PHImageManager defaultManager] requestLivePhotoForAsset:asset targetSize:[UIScreen mainScreen].bounds.size contentMode:PHImageContentModeDefault options:options resultHandler:^(PHLivePhoto * _Nullable livePhoto,NSDictionary * _Nullable info) { if(livePhoto){ NSArray* assetResources = [PHAssetResource assetResourcesForLivePhoto:livePhoto]; PHAssetResource* videoResource = nil; for(PHAssetResource* resource in assetResources){ if (resource.type == PHAssetResourceTypePairedVideo) { videoResource = resource; break; } } if(videoResource){ [[PHAssetResourceManager defaultManager] writeDataForAssetResource:videoResource toFile:fileUrl options:nil completionHandler:^(NSError * _Nullable error) { if(!error){ completionBlock(fileUrl); }else{ completionBlock(nil); } }]; }else{ completionBlock(nil); } }else{ completionBlock(nil); } }]; }else{ completionBlock(nil); } }
基本上您需要做的是首先需要从PHAsset中获取PHLivePhoto对象.之后,您将必须遍历实时照片中的所有资源资源,并检查它是否为PHAssetResourceTypePairedVideo类型.
如果是的话,你收到了你的视频.现在,您需要将其保存到某个临时目录中,就像我在此处所做的那样,并将此文件用于您可能拥有的任何目的.
要播放此视频,您可以使用以下代码:
NSURL *videoURL = [NSURL fileURLWithPath:fileUrl]; AVPlayer *player = [AVPlayer playerWithURL:videoURL]; AVPlayerViewController *playerViewController = [AVPlayerViewController new]; playerViewController.player = player; [self presentViewController:playerViewController animated:YES completion:nil];
如果您需要任何澄清,请随时询问.
P.S.-我在这个方法中做了一些更改,以删除我的应用程序代码的依赖性,因此上面的代码未经测试,但我觉得它应该按预期工作.