iOS Development tips weekly is a series you can find at the Lynda.com and LinkedIn Learning libraries. The first week of a week’s tip will be available to the public. After that, you will need a subscription to get access to it. Click the image the left to view. You’ll find a transcript below.
One cool feature in iOS 11 Swift playgrounds for iPad is the use of the camera. There’s several ways of accessing the camera, from the complex but powerful AVFoundation
to the much simpler UIImagePickerController
. Let me show you how to quickly add a camera to a iPad playground. If you’ve never used UIImagePickerController
before, you can do this in your Xcode projects too.
Download and open up the example file. I’ve included the basic code for a view controller, a view to display the photo, and added a button with an action to shoot a photo.
I’ll first add a image picker controller to the code, just under the shootButton
declaration
let picker = UIImagePickerController()
This is heavily dependent on delegates. You need two of them. Add to the class UIImagePickerControllerDelegate
and then UINavigationControllerDelegate
.
class CameraViewController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate{
You don’t do anything with the second, it just sits there. You do need to declare the delegate for the UIImagePickerController
, so slide down to viewDidLoad
and add
picker.delegate = self
To shoot the photo, head up to the action shootPhoto
. Picker controllers have several modes, but we’re going to stick with simple photos. You do that with the sourceType
and cameraCaptureMode
properties.
picker.sourceType = .camera picker.cameraCaptureMode = .photo
This gets presented as a modal. Always use the full screen for this and present the camera.
picker.modalPresentationStyle = .fullScreen present(picker, animated: true, completion: nil)
There’s two buttons on the camera. One is a Cancel button and the other is the shutter. Both are controlled by delegates. At the bottom of the code You’ll see a place for delegates. For canceling I use the imagePickerControllerDidConcel
delegate method, which I’ll simply dismiss the modal.
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { dismiss(animated: true, completion: nil) }
I’ll add the second method for the shutter imagePIckerController(picker:didFInishPIckingMediaWithInfo:)
.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
The method has a dictionary info. One of the dictionary entries is the original image, which you get with the key UIImagePickerControllerOriginalImage
. I then downcast that to UIImage
.
let newImage = info[UIImagePickerControllerOriginalImage] as! UIImage
I have the image now, and I’ll assign it to the image view I have in my app.
imageView.image = newImage dismiss(animated: true, completion: nil) }
Finally, dismiss the camera
dismiss(animated: true, completion: nil)
Run the playground.
Expand out the live view to full view.
Tap the Shoot Photo button and you’ll probably get asked for permission to take a photo.
Hit OK and you’ll see the camera appear.
Take a photo. It asks if you want to use or retake it,
I’ll use the photo, which shows up in the image view.
Like any app that uses a camera, you’ll notice it popped up the privacy message. I didn’t put code in this for telling the user that the camera is not available nor checking your privacy settings. Depending on your version of playgrounds, you might be able change this in Settings>Privacy>Camera.
You can use this same code of course in a phone or iPad app. Be aware there are some serious limitations to the UIImagePickerController
as a camera for both Xcode and playgrounds. The most critical limitation is you get one orientation, landscape for iPad and portrait for iPhone. You can’t add Augmented reality or other functions to the camera. However for a quick camera, this is great option to use.
The Whole Code
You can download the completed project from GitHub. Here is the code for the project
//:# My Playground Camera //: A demonstration of UIImagePickerController on iPad Playgrounds. // (c)2018 makeapppie.com import UIKit import PlaygroundSupport class CameraViewController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate{ var imageView = UIImageView() var shootButton = UIButton() let picker = UIImagePickerController() @IBAction func shootPhoto(sender:UIButton){ picker.sourceType = .camera picker.cameraCaptureMode = .photo picker.modalPresentationStyle = .fullScreen present(picker, animated: true, completion: nil) } override func viewDidLoad() { super.viewDidLoad() setup() picker.delegate = self } func setup(){ //Set background color to light gray view.backgroundColor = UIColor(white: 0.85, alpha: 1.0) // Set up the image view as a background imageView.contentMode = .scaleAspectFit imageView.frame = view.frame view.addSubview(imageView) //Set up the camera start. shootButton.setTitle("Shoot Photo", for: .normal) shootButton.titleLabel?.font = UIFont(name: "Avenir Next", size: 32) shootButton.addTarget(self, action: #selector(shootPhoto(sender:)), for: .touchUpInside) shootButton.backgroundColor = UIColor(white: 0.1, alpha: 0.3) var shootFrame = view.frame shootFrame.size = CGSize(width: shootFrame.width, height: shootFrame.height * 0.1) shootButton.frame = shootFrame view.addSubview(shootButton) } //: Delegates func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { dismiss(animated: true, completion: nil) } func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { let newImage = info[UIImagePickerControllerOriginalImage] as! UIImage imageView.image = newImage dismiss(animated: true, completion: nil) } } let cameraVC = CameraViewController() PlaygroundPage.current.liveView = cameraVC
Leave a Reply