在swift中使用PHImageManager(requestAVAssetForVideo)



我使用objective-c图像选择器图像选择器来选择一个时间间隔,但我使用swift。我只能访问objective-c代码,所以我使用了objective-c到swift的转换器,但我有一个错误。

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])->Void"的值转换为所需的参数类型"(AVsset?,AVAudioCix?,[NSOObject:AnyObject】?)->Void'

查看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

注意在预期位置添加了?

从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)")
   }
}

相关内容

  • 没有找到相关文章

最新更新