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

iOS

How can I code for an app to automatically load into the iPhones camera (being the apps default screen)

I was wondering what the code is for an app to access the iPhones camera (main.storyboard of app) with other declarations that would be within the UIImagePickerController, so the app loads into the phone camera automatically from the app icon in home screen ?

and would I put it in the viewDidLoad initialiser of viewController

8 Answers

Jeff McDivitt
Jeff McDivitt
23,970 Points

Your complete code should look something like this you need to include the proper delegates

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
    let imagePicker = UIImagePickerController()
    @IBOutlet weak var imageViewer: UIImageView!

    override func viewDidLoad() {
     super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

 func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
        dismissViewControllerAnimated(true, completion: nil)
        imageViewer.image = image
    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
        dismissViewControllerAnimated(true, completion: nil)
        imageViewer.image = image
    }
}

Thank you, the code you have put down compiles but when I try and run the app it says 'build successful' but then when running the app it comes up with some sort of error in Thread 1 or something Here's code: class AppDelegate: UIResponder, UIApplicationDelegate { - Error is Thread 1: signal SIGABRT ??

what should I do because app doesn't run completely yet ?

Jeff McDivitt
Jeff McDivitt
23,970 Points

Hi - Loading it in viewDidLoad will not work, but here is what you will need to do

override func viewDidAppear(animated: Bool) {

    if UIImagePickerController.isCameraDeviceAvailable( UIImagePickerControllerCameraDevice.Front) {
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
        presentViewController(imagePicker, animated: true, completion: nil)
    }
}

Thank you, but also at the moment the imagePicker is an unresolved identifier. How do I resolve this as I've tried stating imagePicker but not working so how do I code for that ? Thanks again

Jeff McDivitt
Jeff McDivitt
23,970 Points

Did you add the UIPickerControllerDelegate?

I have now but more errors have occurred, it says "ViewController does not conform to protocol NSObjectProtocol" and imagePicker is still unidentified as well as presentViewController?? Don't know why I'm struggling with this so much but you are being very helpful, thanks

import UIKit

class ViewController: UIImagePickerControllerDelegate {

func viewDidAppear(_animated: Bool) {


    if UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.front) {
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        presentViewController(imagePicker, animated: true, completion: nil)
    }

func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

}

Jeff McDivitt
Jeff McDivitt
23,970 Points

There error is caused because you have a UI Element that is not connected

I think this was caused because it made me change the last bit of code to imagePickerControllerDidCancel instead of just imagePickerController but when I try and keep it the same as your code, it brings up error that I've already previously declared that code from the 1st 'func imagePickerController' code and build fails completely. Is there something you missed out or how does your code work an not mine??

Jeff McDivitt
Jeff McDivitt
23,970 Points

Mine works. You sure there is not an error somewhere else in your program?

I just tried building it without the last bit of code so there was no redeclaration of imagePickerController, and it came up with the same error as when I had the last bit as imagePickerControllerDidCancel, which you said something isn't connected. Can you see if there is anything not connected, ill show you my code:

import UIKit

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate { let imagePicker = UIImagePickerController() @IBOutlet weak var imageViewer: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
    dismiss(animated: true, completion: nil)
    imageViewer.image = image

}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
    dismiss(animated: true, completion: nil)
    imageViewer.image = image

}

}

Jeff McDivitt
Jeff McDivitt
23,970 Points

I cannot see that by just your code, typically this happens when one of you labels or image view view or something is not connected. Just double check everything is in Xcode

Ok thanks, while I figure out this code and correct it, one question: why have you actually stated the exact same piece of code twice?

Jeff McDivitt
Jeff McDivitt
23,970 Points

That was my mistake it should only be in there one time

the build succeeds now but when attaching the code to run it on iPhone, it came up with "error: attach by pid '53843' failed - unable to attach" ?? don't know what this means