Capture Photo
Instagram Firebase
Using our camera, we can capture what it sees and present it to the user as an image inside a UIImageView component. This will require a separate component that also contains controls that allow us to cancel or save the image into our device's albums. If the user decides to cancel, we'll simply bring the view back to the camera and let the user capture another photo.

Comments (12)
Farukh
6 years ago
HI Brian, jpegPhotoDataRepresentation i deprecated in ios 11... Can I solved it please? Thanks in advance and have a nice day :)
Farukh
6 years ago
I hope that this guy is the way: func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) { guard let imageData = photo.fileDataRepresentation() else { return } let previewImage = UIImage(data: imageData) let containerView = PreviewPhotoContainerView() containerView.previewImageView.image = previewImage view.addSubview(containerView) containerView.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0) } Unfortunaley I can't test ths for now because I'm out for work... Is that the right way?
yjzhang
6 years ago
If you press really quickly, the camera with sometimes take two photos. To prevent that you can add capturePhotoButton.isEnabled = false inside of handleCapturePhoto() and true at end of capture(....). Hope this helps some of you guys make it more polished.
benpalmer661
6 years ago
will the app crash if this is done on computer with a simulator as i'm getting the error [AVCapturePhotoOutput capturePhotoWithSettings:delegate:] No active and enabled video connection' also can this be solved by putting a guard let statement somewhere?
Brian Voong
6 years ago
nicsgodlike@gmail.com
5 years ago
Hi Guys, If you guys having problems on the previewFormatType because of swift 4 bugs this is a workaround @objc func handleCapturePhoto() { print("Capturing photo...") let settings = AVCapturePhotoSettings() #if (!arch(x86_64)) guard let previewFormatType = settings.__availablePreviewPhotoPixelFormatTypes.first else { return } settings.previewPhotoFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewFormatType] output.capturePhoto(with: settings, delegate: self) #endif }
CoachK
5 years ago
Thanks bro
leandroare
5 years ago
let imageData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer!, previewPhotoSampleBuffer: previewPhotoSampleBuffer!) XCODE DADI : 'jpegPhotoDataRepresentation(forJPEGSampleBuffer:previewPhotoSampleBuffer:)' was deprecated in iOS 11.0: Use -[AVCapturePhoto fileDataRepresentation] instead. HOW CAN I REPLACE AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer!, previewPhotoSampleBuffer: previewPhotoSampleBuffer!) thanks
minhoigoo
4 years ago
@available(iOS 11.0, *) func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) { let imageData = photo.fileDataRepresentation() let previewImage = UIImage(data: imageData!) let containerView = PreviewPhotoContainerView() containerView.previewImageView.image = previewImage view.addSubview(containerView) containerView.anchor(top: view.topAnchor, leading: view.leadingAnchor, bottom: view.bottomAnchor, trailing: view.trailingAnchor, paddingTop: 0, paddingLeading: 0, paddingBottom: 0, paddingTrailing: 0, width: 0, height: 0) let previewImageView = UIImageView(image: previewImage) }
Tokyojogo
5 years ago
Hi Brian, 'jpegPhotoDataRepresentation(forJPEGSampleBuffer:previewPhotoSampleBuffer:)' was deprecated in iOS 11.0: Use -[AVCapturePhoto fileDataRepresentation] instead. So I used let imageData = AVCapturePhoto.fileDataRepresentation(???) - what do I put inside the () It says AVCapturePhoto. Tried AVCapturePhoto, photoSampleBufer, previewPhotoSampleBuffer, etc. but cant seem to get it to work. And your final file still has jpegPhotoDataRepresentation so we need to change it. Thanks.
Brian Voong
5 years ago
Tokyojogo
5 years ago
Thank you Brian, that worked.
DHM
5 years ago
@objc func handleCapturePhoto() { print("Capturing photo...") var settings = AVCapturePhotoSettings() settings.previewPhotoFormat = settings.embeddedThumbnailPhotoFormat output.capturePhoto(with: settings, delegate: self) } func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) { guard error == nil else { print("Fail to capture photo: \(String(describing: error))") return } guard let imageData = photo.fileDataRepresentation() else {return} let previewImage = UIImage(data: imageData) let previewImageView = UIImageView(image: previewImage) view.addSubview(previewImageView) previewImageView.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0) print("finish processing photo sample buffer ...") } try this it'll work
drestaccz
5 years ago
can you share the code that worked for this problem cause it keep crashing for me
LIU CHA WEI
4 years ago
Thanks. in iOS12.0 , it's work .
Brian Voong
4 years ago
kelleymuro
5 years ago
The preview image gets sort of squised from the left and right side, how do I prevent this from happening? Something like resizeAspectFill on previewImageView?
Brian Voong
5 years ago
kelleymuro
5 years ago
Sweet! iv.contentMode = .scaleAspectFill is just what I needed! You're the bomb! Have some questions about creating image & video filters with LUTs, is this something we could go over if I purchase an hour call with you?
ronaldruck@live.com
4 years ago
Brian, do you offer one hour call consultation? if so, i am interested cause i need help
Brian Voong
4 years ago
Александр Коловатов
5 years ago
Hi, Brian! First of all, great thanks to you for your big deal! I've seen many of your youtube videos. These are very helpful for me! I just want to ask will you make a course for advanced - to - pro? I would buy it! Thank you..
Brian Voong
4 years ago
BTol Mohammad
4 years ago
Hi Brian, 'jpegPhotoDataRepresentation(forJPEGSampleBuffer:previewPhotoSampleBuffer:)' was deprecated in iOS 11.0: Use -[AVCapturePhoto fileDataRepresentation] instead. How can I solve it ?
BTol Mohammad
4 years ago
This is the error that I get: 0x1f7174bc8 <+212>: mov x0, x20 Thread 1: signal SIGABRT
Brian Voong
4 years ago
TWei
4 years ago
Replace func photoOut() with this one. func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) { let imageData = photo.fileDataRepresentation() let previewImage = UIImage(data: imageData!) let previewImageView = UIImageView(image: previewImage) view.addSubview(previewImageView) previewImageView.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0) print("Finish processing photo sample buffer...") }
anantangad
4 years ago
Hey Brian, This is what I'm trying to do: When I click the plus button, I need the camera to come up, which is working perfectly, but when I click on the cancel button on the camera, I need the home controller with the feeds to show up. Currently when I click the cancel button I get a black screen, but I want the home feed to show up. How can I do that? Thanks.
Brian Voong
4 years ago
anantangad
4 years ago
I think it is the plusNavController which was created using the template method you showed.
anantangad
4 years ago
Hey Brian, I think it is the plusNavController which was created using the template method you showed.
Brian Voong
4 years ago
Cinquain
4 years ago
That video was lit. Brian you should create a video on how to read documentations as effectively as you do on your regular YouTube channel
HELP & SUPPORT