iosswiftphassettimelapse

Using PHImageManager in swift (requestAVAssetForVideo)


I am using an objective-c image picker image picker to pick a time lapse but I am doing it in swift. I only had access to the objective-c code so I used a objective-c to swift converter and I have an error.

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

This is the code I have currently. The error I am receiving is below.

Cannot convert value of type '(AVAsset, AVAudioMix, [NSObject : AnyObject]) -> Void' to expected argument type '(AVAsset?, AVAudioMix?, [NSObject : AnyObject]?) -> Void'


Solution

  • Look at the docs for requestAVAssetForVideo:options:resultHandler: The resultHandler you have doesn't match the required signature.

    Change this line:

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

    to:

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

    Note the addition of the ? in the expected places.