Swift 从照片库中获取视频的 NSData [英] Swift get NSData of a video from photos library

查看:162
本文介绍了Swift 从照片库中获取视频的 NSData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im using UIImagePickerController to select a video from my library. I need to extract the NSData of my video file. Im using the following operation to select a video from my library but my data appears to be nil however my AVPlayer plays the video from the resulting NSURL so I know problem is not with the NSURL. How can I extract the NSData of my selected video file?

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

推荐答案

尝试将 UIImagePickerControllerReferenceURL 更改为

Try changing UIImagePickerControllerReferenceURL to

UIImagePickerControllerMediaURL

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

Instead of let data = NSData(contentsOfURL: fileURL)

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

这篇关于Swift 从照片库中获取视频的 NSData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆