如何从视频网址获取NSData?

时间:2016-08-23 18:32:45

标签: ios swift video nsdata

我试图从UIImagePicker获取视频,并将其作为数据发送到我的restkit API。

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

var movieData: NSData?
        do {
            movieData = try NSData(contentsOfURL: videoPath, options: .DataReadingMappedIfSafe)
        } catch {
            print(error)
            movieData = nil
            return
        }

// Multipart Form Request for RestKit POST goes here

        imagePickerController.dismissViewControllerAnimated(true, completion: nil)
    }

收到以下错误:

  

错误域= NSCocoaErrorDomain代码= 256"无法打开文件“asset.MOV”。" {?NSURL =资产库://asset/asset.MOV ID = B906AB1B-F311-47BA-8FE9-EE7AADDB7796&安培; EXT = MOV}的UserInfo =

2 个答案:

答案 0 :(得分:5)

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
            let videoURL = info["UIImagePickerControllerMediaURL"] as? NSURL
             do {
                let video = try NSData(contentsOfURL: videoPath, options: .DataReadingMappedIfSafe)
             } catch {
                print(error)
                return
             }
            imagePickerController.dismissViewControllerAnimated(true, completion: nil)
    }

答案 1 :(得分:1)

以下是我如何处理来自设备的视频:

@IBAction func addVideoFromDevice(_ sender: AnyObject) {
    let mediaUI = UIImagePickerController()
    mediaUI.sourceType = .savedPhotosAlbum
    mediaUI.mediaTypes = [kUTTypeMovie as NSString as String]
    mediaUI.allowsEditing = true
    mediaUI.delegate = self
    present(mediaUI, animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    let mediaType = info[UIImagePickerControllerMediaType] as! NSString
    if mediaType == kUTTypeMovie {
        let videoURL = info[UIImagePickerControllerMediaURL] as! NSURL
        let avAsset = AVAsset(url: videoURL as URL) //>selected video
    } else{
        print("error @ mediaType-didFinishPickingMediaWithInfo")
    }
    dismiss(animated: true, completion: nil)
}

希望有所帮助:)