在swift中使用PHImageManager(requestAVAssetForVideo)

时间:2015-12-18 21:28:08

标签: ios swift phasset timelapse

我正在使用一个Objective-c图像选择器图像选择器来选择一个时间流逝,但我正在迅速做到这一点。我只能访问objective-c代码所以我使用了一个objective-c来快速转换器,我有一个错误。

func qb_imagePickerController(imagePickerController: QBImagePickerController, didFinishPickingAssets assets: [AnyObject]) {
    for asset in assets as! [PHAsset] {
        PHImageManager.defaultManager().requestAVAssetForVideo(asset, options: nil, resultHandler: {(avAsset: AVAsset, audioMix: AVAudioMix, info: [NSObject : AnyObject]) -> Void in
            var url: NSURL = NSURL(string: "\(asset)")!
            var avAsset: AVURLAsset = AVURLAsset(URL: url, options: nil)
            var playerItem: AVPlayerItem = AVPlayerItem(asset: avAsset)
            var videoPlayer: AVPlayer = AVPlayer(playerItem: playerItem)
            videoPlayer.play()
        })
    }
    self.dismissViewControllerAnimated(true, completion: { _ in })
}

这是我目前的代码。我收到的错误如下。

  

无法转换类型的值#(AVAsset,AVAudioMix,[NSObject:AnyObject]) - >虚空'预期的参数类型'(AVAsset?,AVAudioMix?,[NSObject:AnyObject]?) - >空隙'

2 个答案:

答案 0 :(得分:2)

查看requestAVAssetForVideo:options:resultHandler:的文档resultHandler您所拥有的文件与所需的签名不匹配。

更改此行:

PHImageManager.defaultManager().requestAVAssetForVideo(asset, options: nil, resultHandler: {(avAsset: AVAsset, audioMix: AVAudioMix, info: [NSObject : AnyObject]) -> Void in

为:

PHImageManager.defaultManager().requestAVAssetForVideo(asset, options: nil, resultHandler: {(avAsset: AVAsset?, audioMix: AVAudioMix?, info: [NSObject : AnyObject]?) -> Void in

请注意在预期位置添加?

答案 1 :(得分:0)

从QBImagePickerController中的didFinishPickingAssets从Swift 4.2中的PHAsset获取视频URL

let imagePicker = QBImagePickerController()
imagePicker.delegate = self
imagePicker.numberOfColumnsInPortrait = 2
imagePicker.maximumNumberOfSelection = 2
imagePicker.allowsMultipleSelection = true
imagePicker.mediaType = .video

self.present(imagePicker, animated: true, completion: nil)

func qb_imagePickerController(_ imagePickerController: QBImagePickerController!, didFinishPickingAssets assets: [Any]!) {
     for asset in assets
     {
        PHCachingImageManager().requestAVAsset(forVideo: asset as! PHAsset, options: nil, resultHandler: {(asset, audioMix, info) -> Void in
        if asset != nil {
            let avasset = asset as! AVURLAsset
            let urlVideo = avasset.url
            yourarray.add(urlVideo as URL)
        }

       })

    }


   imagePickerController.dismiss(animated: true) {
       //You can access video urls here
       print("Total Videos:\(yourarray.count)")
   }

}