Swift从照片库中获取视频的NSData

时间:2016-07-10 12:43:11

标签: swift uiimagepickercontroller nsdata nsurl

我正在使用UIImagePickerController从我的库中选择一个视频。我需要提取我的视频文件的NSData。我使用以下操作从我的库中选择一个视频,但我的数据似乎是零,但我的AVPlayer播放结果NSURL中的视频,所以我知道问题不在于NSURL。如何提取所选视频文件的NSData?

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    let fileURL = info[UIImagePickerControllerReferenceURL] as! NSURL!

    let data = NSData(contentsOfURL: fileURL)
    print(data)

    player = AVPlayer(URL: fileURL)
    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = CGRectMake(0, 0, 300, 300)
    self.view.layer.addSublayer(playerLayer)
    player.play()

    self.dismissViewControllerAnimated(true, completion: nil)
}

1 个答案:

答案 0 :(得分:7)

尝试将UIImagePickerControllerReferenceURL更改为

  

UIImagePickerControllerMediaURL

而不是让data = NSData(contentsOfURL:fileURL)

  

让video3 =尝试NSData(contentsOfURL:videoDataURL,选项:   .DataReadingMappedIfSafe)