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

The Quotation Bank App — Bringing daily English Literature insights to students

We’re excited to share that **The Quotation Bank App** — developed in partnership with *The Quotation Bank* — is now live on the **App Store and Google Play**.

Read more

Enabling Apple Pay within Stripe Connect accounts

We've been working on a SaaS product for one of our clients and decided to use Stripe Connect to manage platform payments. Stripe Connect enables organizations onboarded into the…

Read more

Ready when you are

Let’s build something together

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