Picking images & videos from the iOS photo album with RubyMotion

by Luke, Founder

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

More articles

Why engineering process is becoming a consultancy’s biggest advantage

As AI makes code faster to produce, the real differentiator for software consultancies is shifting to the engineering process around it.

Read more

Why mobile apps need ongoing support

Getting a mobile app into the App Store and Google Play is not the end of the story. Ongoing support keeps it compatible, reliable and store-ready as platforms change.

Read more
The Azuki team smiling and gathered together, ready to build and collaborate with clients.

Ready when you are

Let’s build something together

Whether you’re starting fresh or looking to improve things, we’re ready to talk.