Showing Camera
Instagram Firebase
Up to this point, our application is only able to send photos that are already on the device. Wouldn't it be nice if we could capture photos using the Camera on our phones? In this lesson, let's setup the necessary AVFoundation components that will grant us access to the photo features of the device. There is a bit of setup work involved before we can fire up the camera so be patient in this lesson.

Comments (30)
Nathan Hsiao
6 years ago
hi brian, avcapturephotooutput only work for ios 10 can you show us the alternative for ios 9
Brian Voong
6 years ago
Nathan Hsiao
6 years ago
Thank You for the reply, my iphone still running ios 9 , wondering should i stick with ios 9 with jb or just update it to ios 10, but shouldnt the app should able to support at least ios 9 +.
Brian Voong
6 years ago
Maciek Górecki
6 years ago
There is a very weird camera bug when you rotate your phone to landscape.
Jesus Adolfo
6 years ago
is there any way to run the camera directly on the simulator? Like in Android there is a dummy camera of some sort or you can even do some stuff to use the webcam in your laptop
Brian Voong
6 years ago
Aurélien Haie
5 years ago
Does it mean that it is not supposed to work on simulator? because the captureDevice returns nil in my case and i do use simulator, I am wondering if it's normal..
jacquesj
6 years ago
Hello Brian, I have a question regarding lazy vars. In UserProfileHeaderCollectionViewCell we had to make the editProfileOrFollowButton a lazy var so that we could add a targetAction, but on other buttons such as the ones in today's lesson we didn't need to add the lazy keyword even though we added a targetAction. Can you please explain to me when to use the lazy keyword regarding targetActions? Thank you Brian.
Brian Voong
6 years ago
wind
6 years ago
Hello Brian, I already add Privacy - Camera Usage Description in Info.plist, after run app from simulator then I got this error : Could not setup camera input: Error Domain=AVFoundationErrorDomain Code=-11814 "Cannot Record" UserInfo={NSLocalizedDescription=Cannot Record, NSLocalizedRecoverySuggestion=Try recording again.} How to solve this error, Thank you
Burak Ahmet Şentürk
6 years ago
Because you running on emulator.If you can run on phone,problem will fix
Chard
6 years ago
how to find the camera api demo step by step
Brian Voong
5 years ago
Chard
5 years ago
right right awsome
Hursh Hazari
5 years ago
Hi Brain, I have followed all the steps till setting up the previewLayer, adding the property to info.plist file and running the app on my phone. However, I am unable to get the preview from the capture, and just see the black view controller background. I am running this on Xcode 9.2 Beta
Brian Voong
5 years ago
Hursh Hazari
5 years ago
I have been having this issue for a while. Since the Beta download, I’m unable to use Th camera for another project as well. I update the info.plisy file, create an instance of captureSession, add the inputs and outputs an previewLayer and eventually start the session. However it still shows the default black screen. Can I email you my project and perhaps you can help me?
Hursh Hazari
5 years ago
I cleaned and ran the project again. Now, it’s working. This is an awesome course Brain. The quality and explanation of content is excellent
diehard98
5 years ago
Hi, Brian For downloaded CameraController.swift code for latest version of Firebase, when you declare previewLayer in setupCaptureSeesion() function, you did not use 'guard' and 'return'. I guess it is missed? Thanks.
Brian Voong
5 years ago
diehard98
5 years ago
Ah, I see. :) So it was safe to use it that way. Thank you for quick reply!
Akib11
5 years ago
Hi, Brain On iPhone x output preview has top and bottom space. How to make output preview fullscreen on iPhone x ? //3. setup output preview let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) previewLayer.frame = view.frame view.layer.addSublayer(previewLayer) Thanks
awaghmare
5 years ago
If you set the videoGravity property of the previewLayer to aspect fill, it will fill up the whole screen: previewLayer.videoGravity = .resizeAspectFill
h01m3s
5 years ago
Thank you. That works.
HappyCow
3 years ago
I tried this, but it's not working. I still get a white space between the previewLayer and the top bar that shows the time and battery. How do I fix it?
jjkrall
5 years ago
Hey Brian, trying to make a slight modification to your code for our application. We would like to display the UserSearchController in place of your CameraController. Using similar code to yours we get a SIGABRT crash in our app delegate. Is this error because the Search Controller is of type UICollectionViewController, preventing us from presenting it as a UIViewController? Any thoughts on a fix would be greatly appreciated!
Ashim Dahal
5 years ago
hey, ​Brian don't I need developer certificate to run an app on the device? I don't have a certificate from apple ....
Ashim Dahal
5 years ago
I found solution ..with registration process for device. .
Flip902
5 years ago
Hey Brian, for some reason when I post a photo on my phone it shows up twice in the feed, but in the simulator everything is fine, any Idea as to why this is happening? I've been trying to debug this myself for an hour now.
Brian Voong
5 years ago
tetraprism95
5 years ago
It's possible that you need to change to observeSingleEventType() from observe, in your HomeController
jayrsonani
5 years ago
hey Brian Where is others features like boomreag photo effect??
yoowinks
5 years ago
Really appreciate this course, Brian. Thank you for taking the time to make it. Looking forward to purchasing the Podcasts one after this one is completed. Just a tip that may help anyone trying to complete the following line in Swift 4.1. In Brian's lesson, the line is: let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) For Swift 4.1, I found the line to be: guard let captureDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back) else { return } Note the AVCaptureDevice returned is actually an optional hence the 'guard let' and the built in camera is the Wide Angle not the Dual camera. (This is what took me the longest to figure out why the app didn't crash as expected, but Google saved the day).
rehan1531
5 years ago
thanks
Chris Del Rosario
5 years ago
Hi Brian, I'm trying to understand views and layers and got curious ... What is the difference of adding previewLayer (CALayer) as a "sublayer" of the view as opposed to creating a custom UIView (containing an AVCaptureVideoPreviewLayer) and adding it as a subview. I ran into the "adding as a subview" method while reading the Apple documentation for "Setting up a Capture Session" I tested both approach and they seem to produce the same result.
Jaylon22
5 years ago
Hi Brian, I love your tutorial i do have one quick question. I've got the camera to show but when i log the user out and log back in it seems like the camera screen is frozen from the last frame it received. Basically to make the question more understandable how can i make the camera update when the user logs out and log back in, and go to the camera. Thank you.
Jaylon22
5 years ago
Its like the camera view freezes and never updates
Brian Voong
5 years ago
Derik Malcolm
5 years ago
Hi Brian, could you go over making the transition interactive?
Rob Wise
5 years ago
Hi Brian I have been going through your tutorials and i have an idea for an app but am still learning is i was trying to have the camera detect an image like say a hand and then take a picture of it automatically i also have not been able to find a great deal of info on this so any help is def apreciated
Franck I-n
5 years ago
I'm guessing this can be done using CoreML, where you would let the camera detect objects. He has a tutorial on his youtube channel. From there you could write an IF statement where if the ML prediction contains "hand", your camera would take a picture. That's how I would do it​ anyways. There might be a better way but the method above should work.
ngsison
4 years ago
Hi Brian Is there any way we can enlarge the capture button image without having to move the actual asset to 1X?
Brian Voong
4 years ago
ngsison
4 years ago
Hi Brian. What I mean is not the bar button in homecontroller. It’s actually the capture button that will actually do the photo capturing, not the showing of cameracontroller. It’s just a normal button
ngsison
4 years ago
The circle button at the bottom of cameracontroller
Brian Voong
4 years ago
ngsison
4 years ago
Hi Brian, it works by setting the width and height anchor of circleButton.imageView equal to circleButton's width and height anchor :/ I tried other solutions like: circleButton.imageEdgeInsets = .zero circleButton.imageView.bounds = circleButton.bounds doesn't work hehe
Brian Voong
4 years ago
Kevin Jefferson
4 years ago
hello Brian, what is the function of the send icon in the top right? and in which video can this function be found??
Kevin Jefferson
4 years ago
the paper airplane icon*
Brian Voong
4 years ago
Kevin Jefferson
4 years ago
yes thank you Brian will do :)
anantangad
4 years ago
Hello Brian, Can you show how to implement the options button?
Brian Voong
4 years ago
anantangad
4 years ago
The one on every home post cell, which has text as •••.
anantangad
4 years ago
Hello Brian, Can you show how to implement the options button? The one on every home post cell, which has text as •••.
Brian Voong
4 years ago
anantangad
4 years ago
Hey Brian, I remember you discussed how to remove unnecessary statements from the console, but I don't know in which video. Could you please tell me which video it was, can't find it. Thanks.
smiller193
4 years ago
Hello Brian , I appreciate this course a lot but I just have a quick question. A lot of this code works until I have to use the iPhone X and then the camera is zoomed in so closet is almost non functional. Is there anyway that I can make it so it works with all iPhones?
MaxApp
4 years ago
Hey Brian, I hope you are having a good day! Are you using any architecture for this project? Because I keep hearing people talking about MVC and MVVM.
Brian Voong
4 years ago
M Nisar
4 years ago
hi Brian Voong, hope you will be fine and i wish you good luck and i want to ask you something about video player if you don't mind, as I'm stuck in building the facebook or instagram like video player can you please help me out in that will be more appreciated if i can get some tips about that like how to make an player on lite browsing speed and play etc..
amsc
4 years ago
How do I constraint the HUD to the safe area? it doesn't look good on the iphone x
Hans Nice
3 years ago
where is download link?
Brian Voong
3 years ago
Hans Nice
3 years ago
I saw a button "Download Project" before login, but it's disappeared after i logged in here. Is it a trick for a user to sign up this site?
Hans Nice
3 years ago
I saw a button "Download Project" before login, but it's disappeared after i logged in here. Is it a trick for a user to sign up this site?
Brian Voong
3 years ago
Hans Nice
3 years ago
Please rename it as "Purchase to download project"
Brian Voong
3 years ago
Arif Calhan
3 years ago
how can we display live names on the app?
samu
2 years ago
sos mierda
HELP & SUPPORT