如何在Swift iOS中将视频PHAsset转换为视频URL

时间:2018-09-29 07:31:27

标签: ios video swift3 phasset avurlasset

我正在使用AssetsPickerViewController库来选择Videos的倍数。在这里,我得到[PHAsset]的数组:

func assetsPicker(controller: AssetsPickerViewController, selected assets: [PHAsset]) {
        self.assets = assets
}

当我打印self.assets[0]时,我会得到输出:

<PHAsset: 0x105230b40> 49185CD0-E5D7-4A84-BB75-38DB58C2EB20/L0/001 mediaType=2/0, sourceType=1, (1920x1080), creationDate=2018-03-17 02:35:36 +0000, location=0, hidden=0, favorite=0 

现在,我的问题是,当我尝试从self.assets[0]获取视频URL时,由于没有资产,我的应用程序崩溃了。我尝试了以下解决方案,但所有解决方案都不适合我。

Sol 1:

PHCachingImageManager().requestAVAsset(forVideo: assets[0], options: nil, resultHandler: {(asset: AVAsset?, audioMix: AVAudioMix?, info: [NSObject : AnyObject]?) in

            let asset = asset as! AVURLAsset
             DispatchQueue.main.async {
                print(asset.url)
            }
            } as! (AVAsset?, AVAudioMix?, [AnyHashable : Any]?) -> Void)

Sol 2:

PHImageManager.default().requestAVAsset(forVideo: self.assets[0], options: nil, resultHandler: { (asset, mix, nil) in
            let myAsset = asset as? AVURLAsset
            do {
                let videoData = try Data(contentsOf: (myAsset?.url)!)
                print("video data : \(videoData)")

            } catch  {
                print("exception catch at block - while uploading video")
            }
        })

Sol 3:

PHImageManager.default().requestAVAsset(forVideo: assets[0], options: options, resultHandler: { (asset, audioMix, info) in
            if let urlAsset = asset as? AVURLAsset {
                let localVideoUrl = urlAsset.url
                print("VIDEO URL: ", localVideoUrl)
            } else {
                //nil
            }
        })

有人可以在这里建议我吗,我在这里想念东西吗?

0 个答案:

没有答案