如何从iOS中的实时照片获取视频 [英] How to Get Video from a Live Photo in iOS

查看:134
本文介绍了如何从iOS中的实时照片获取视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出答案,但是找不到任何有用的信息. 我只发现了这个:

I'm trying to figure it out, but can't find any useful information. I only found this:

PHAssetResourceManager.defaultManager().writeDataForAssetResource(assetRes, 
toFile: fileURL, options: nil, completionHandler: 
{
     // Video file has been written to path specified via fileURL
}

但是我很to愧地说我不知道​​如何发挥作用. 我已经创建了UIImagePickerController并从相机胶卷"中加载了图像.

but I'm ashamed to say I have no idea how to play it out. I've created a UIImagePickerController and loaded an Image from the Camera Roll.

推荐答案

使用以下代码从实时照片获取视频:

Use this code to get the video from live photo:

- (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类型.

Basically what you have to do is that you first need to fetch the PHLivePhoto object from your PHAsset. After that, you will have to traverse all the asset resources within your live photo and check if it is of type PHAssetResourceTypePairedVideo.

如果是,则获得了视频.现在,您将需要像我在这里一样将其保存到某个临时目录中,并将此文件用于您可能有的任何用途.

If yes, you got your video. Now you will require to save it to some temporary directory as I did here and use this file for whatever purpose you may have.

要播放此视频,可以使用以下代码:

To Play this video, you can use the following code:

NSURL *videoURL = [NSURL fileURLWithPath:fileUrl];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:nil];

随时询问您是否需要任何澄清.

Feel free to ask if you need any clarification.

P.S .- .我对该方法进行了一些更改,以消除我的应用程序代码的依赖性,因此上述代码未经测试,但是我认为它应该可以正常工作.

P.S.- I made a few changes in this method to remove dependency of my application's code so the above code is untested, however I feel it should work as expected.

这篇关于如何从iOS中的实时照片获取视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆