Picking images & videos from the iOS photo album with RubyMotion

10 January
  • rubymotion, ios, ruby, technical

camera We’ve been working on a video app for a client of ours. We came across the need to select videos from the iOS camera roll/photo album. It was one of those trial and error type (-frustrating) endeavours. We thought we’d write down our findings in the hope of saving someone else from the same frustration.

As with a lot of things, it ended up being pretty simple in the end.

First we use the UIImagePickerController to check if the source type we need is available and then present the picker. The media types weren’t obvious at first. Check out the docs for more configuration options.

def pick_from_camera_roll
  if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceTypeSavedPhotosAlbum)
    return
  end
  mediaUI = UIImagePickerController.new
  mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum
  mediaUI.mediaTypes = [KUTTypeMovie, KUTTypeVideo, KUTTypeMPEG4]
  mediaUI.allowsEditing = true
  mediaUI.delegate = self
  presentModalViewController(mediaUI, animated: true)
end

Then when that’s complete, you can pick up the URL of the selected video. You can copy it like we do here or do whatever else you need.

def imagePickerController(picker, didFinishPickingMediaWithInfo: info)
  video_url = info.objectForKey UIImagePickerControllerMediaURL
  fileManager = NSFileManager.defaultManager
  fileManager.copyItemAtPath(video_url.path, toPath:"video.mp4".document_path, error:nil)

  picker.dismissViewControllerAnimated(true, completion: nil)

  # any other action
end