如何使用swift从dkimagepicker ios获取视频

时间:2016-08-10 18:34:14

标签: ios swift

我可以使用以下代码成功编写图像并获取图像,

public func writeImageToFile(path: String, completeBlock: (success: Bool) -> Void)

但我无法使用以下方式编写和播放视频:

  func showImagePickerWithAssetType(assetType: DKImagePickerControllerAssetType,
                                  allowMultipleType: Bool,
                                  sourceType: DKImagePickerControllerSourceType = .Both,
                                  allowsLandscape: Bool,
                                  singleSelect: Bool) {

    let pickerController = DKImagePickerController()
    pickerController.assetType = assetType
    pickerController.allowsLandscape = allowsLandscape
    pickerController.allowMultipleTypes = allowMultipleType
    pickerController.sourceType = sourceType
    pickerController.singleSelect = singleSelect
    pickerController.defaultSelectedAssets = self.assets
    pickerController.didSelectAssets = {

        [unowned self] (assets: [DKAsset]) in

        self.assets = assets


        for asst in self.assets! {

/// [开始:在存储媒体的设备中创建目录]>>>>>>>>

            var dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
            dirPath = dirPath + "/Media/"
            do{
                try NSFileManager.defaultManager().createDirectoryAtPath(dirPath, withIntermediateDirectories: false, attributes: nil)
            }
            catch{
            }

/// [结束:在存储媒体的设备中创建目录]>>>>>>>>

            var fileName = "filename.mov"               
            dirPath=dirPath.stringByAppendingString(fileName)

                fileName = self.Utils.randomVideoName(10) as String
                fileType = "video"
                dirPath=dirPath.stringByAppendingString(fileName)

               asst.writeAVToFile(dirPath, presetName: AVAssetExportPresetLowQuality, completeBlock: { (success) in

//从已保存的文件路径播放视频

                let avPlayerItem = AVPlayerItem(URL: NSURL(string: dirPath)!)      
               // let avPlayerItem = AVPlayerItem(URL: NSURL(fileURLWithPath: path)!)            
                let avPlayer = AVPlayer(playerItem: avPlayerItem)
                let player = AVPlayerViewController()
                player.player = avPlayer                    
                avPlayer.play()                    
                self.presentViewController(player, animated: true, completion: nil)

                })

        }
    }
}

0 个答案:

没有答案