Ben*_*nny 6 ios swift phlivephoto
我想弄清楚,但找不到任何有用的信息.我才发现这个:
PHAssetResourceManager.defaultManager().writeDataForAssetResource(assetRes,
toFile: fileURL, options: nil, completionHandler:
{
// Video file has been written to path specified via fileURL
}
Run Code Online (Sandbox Code Playgroud)
但我很惭愧地说我不知道怎么玩它.我创建了一个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);
}
}
Run Code Online (Sandbox Code Playgroud)
基本上你要做的就是你首先需要PHLivePhoto从你的对象中获取对象PHAsset.之后,您必须遍历实时照片中的所有资源资源,并检查它是否属于类型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];
Run Code Online (Sandbox Code Playgroud)
如果您需要任何澄清,请随时询问.
PS-我在这个方法中做了一些更改,以删除我的应用程序代码的依赖性,所以上面的代码是未经测试的,但我觉得它应该按预期工作.
| 归档时间: |
|
| 查看次数: |
2839 次 |
| 最近记录: |