Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Michael Lee
2,855 PointsNeed help with a custom camera (swift)
How would I code a custom camera in Swift, with a preview of an image, a button to take a photo of that image, and then kind of freezing the image on the screen, while giving the user options to save the image, or retake the photo, etcetera.
I tried searching this up online and I realise that I need to need to create a UIView and set it as a CameraOverlay to the UIImagePicker, or use AVCamera.
However, my code so far only shows a black screen with an image preview taking up about the top half of the screen. The button I created to launch the photo library is not shown or clickable.
class SecondViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
@IBOutlet var imageView: UIImageView! weak var picker: UIImagePickerController!
@IBOutlet var cameraOverlay: UIView!
override func viewDidLoad() { super.viewDidLoad() self.view.addSubview(imageView)
var camera = UIImagePickerController()
camera.delegate = self
camera.allowsEditing = false
camera.sourceType = UIImagePickerControllerSourceType.Camera
camera.showsCameraControls = false
imageView.layer.frame = camera.cameraOverlayView!.frame
camera.cameraOverlayView = imageView
imageView = nil
self.presentViewController(camera, animated: false, completion: nil)
}
override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }
override func viewWillAppear(animated: Bool){ self.navigationController?.navigationBarHidden = true }
@IBAction func openPhotoLibrary(sender: UIButton) {
var photoPicker = UIImagePickerController()
photoPicker.delegate = self
photoPicker.sourceType = .PhotoLibrary
self.presentViewController(photoPicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){
imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismissViewControllerAnimated(false, completion: nil)
}
@IBAction func takePhoto(sender: UIButton) { } }