All right. 1
So the first thing that we need to do is we're going to set up the interface. So UIPickerController 2
requires a navigation controller. 3
So we're going to select it and then embed it in a Navigation Controller. Let's just shift it over to the 4
left there. 5
All right. 6
So the next thing that we need to do is to add a bar button item up here. 7
So let's search for bar button. 8
There we go. Found it. And we are all going to change it to a Camera style. 9
So where's that camera? There we go. 10
And finally, we're going to need a ImageView in order to display the selected image. 11
So we're just going to put that in there. And I'm going to add constraints to all four corners and I'm 12
going to hit enter. There we go. And it's going to give me a warning, so I can get rid of that by just saying, 13
"Use Safe Area Layout Guides," and you can see now the warning is gone. Cool. 14
So we've done our design. It's pretty minimal. But now we just need to link it up into the code. 15
So first thing I'm going to create is an IBAction for my camera and I'm going to call it cameraTapped. 16
Now, you can call it whatever you want as long as it makes sense to you. 17
You can call it whatever. 18
And the other thing I'm going to do is link up 19
this UIImageView. I'm just going to call it ImageView. 20
There we go. 21
All right, cool. 22
So I'm all done with my design and my linking up with code. 23
Next thing I need to do is to code up the actual image picker so that it brings up the camera and allows 24
the usage to take a photo. 25
So the first thing that we need to do to allow our app to use the camera is, of course, giving a privacy 26
description. 27
So if we head into the info.plist, then we can add a new Privacy-Camera Usage Description 28
and I'm just going to say something like, "We need your camera!" 29
All right, so that's done. 30
And now we're going to head into ViewController and code up our UIImagePickerController. 31
First thing I'm going to do, I'm going to make this file a little bit clearer for you to see. 32
And then I'm going to add our UIImagePickerControllerDelegate as well as the 33
UINavigationControllerDelegate. 34
All right. Then I'm going to create a new imagePicker and it's going to be an object of type 35
UIImagePickerController. 36
All right. Then inside my viewDidLoad, I'm going to set my imagePIcker's delegate to self, and then I'm 37
going to implement the imagePicker delegate method which is called didFinishPickingMediaWithInfo. 38
All right. 39
So once we've finished picking media with info, then we want to pick out that image. 40
So, remember, we can tap into the image by using the info dictionary. 41
So let userPickedImage 42
= info. 43
And we're tapping into the key called 44
uiImagePickerControllerOriginalImage. 45
That's quite a mouthful. 46
All right. 47
And we're going to set our imageView's image property to that userPickedImage, and we're going to 48
cast it as a UIImage. 49
All right. 50
So two more things. 51
One is to set some properties on our imagePicker, so imagePicker's allowsEditing, we'll set it as false 52
for now, and then the other thing that we want to set is the sourceType which is going to be .camera. 53
And we need to present our imagePicker onto the screen 54
when the camera is tapped, so present, ViewController, animated, ViewController to present is called 55
imagePicker, animated: 56
true, completion: nil. 57
All right. 58
Done. 59
And we need to remember that once the user is finished picking the image, we need to dismiss this 60
imagePicker. So imagePicker.dismiss. 61
Animated: true, completion: nil. 62
All right. 63
And that's all we need in order to make our UIImagePicker work. 64
So let's go ahead and give it a spin. 65
All right, so here's our app, nice and clean interface, and we're going to tap the camera button to bring up 66
the UIImagePickerController. I'm going to take a photo, and then I'm going to click Use Photo. 67
And as you can see, it's set as the background of my image view, and we've successfully implemented our 68
UIImagePickerController. 69
All right. 70
So I just want to show you one more thing and that's if you do actually want the user to be able to crop 71
the image that they've taken, then you can change 72
this allowsEditing property to true, 73
and also you'll need to change this image, 74
instead of being the UIImageViewControllerOriginalImage, you want the UIImagePickerControllerEditedImage, 75
so that way you can bring up the cropped image. 76
So that's a user experience choice for you, whether if you want your app to allow cropping or not, 77
but it's pretty simple to switch around anyways. 78
And I think in the case of flower recognition because the model was trained on a lot of head-on shots 79
of flowers, it might make sense to crop it into square. 80
But again, it's a choice that you can make, 81
so it works both ways and just decide on what you want for your app. 82
So I'll see you on the next lesson where we use our Flower Classifier to detect the image that's being 83
picked in the UIImagePicker. 84
So I'll see you there.