2016-08-23 12 views
5

私はUIImagePickerからビデオを取得しようとしていて、それをrestkit API全体のデータとして送信しようとしています。ビデオURLからNSDataを取得するにはどうすればよいですか?

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

次のエラーを取得する:ここで

Error Domain=NSCocoaErrorDomain Code=256 "The file “asset.MOV” couldn’t be opened." UserInfo={NSURL=assets-library://asset/asset.MOV?id=B906AB1B-F311-47BA-8FE9-EE7AADDB7796&ext=MOV}

+0

可能な重複NSCocoaErrorDomainコード= 256](http://stackoverflow.com/questions/10106) 087/nscocoaerrordomain-code-256) – JAL

+0

'NSURL(fileURLWithPath:videoPath)' –

答えて

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

それが役に立てば幸い:) [の

4
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) 
    } 
関連する問題