Skip to content

Instantly share code, notes, and snippets.

@vinamelody
Created July 7, 2016 08:07
Show Gist options
  • Save vinamelody/015c24ac72b73f11edadf1481905a112 to your computer and use it in GitHub Desktop.
Save vinamelody/015c24ac72b73f11edadf1481905a112 to your computer and use it in GitHub Desktop.
Swift: Take photo button action
@IBAction func takePhoto(sender: UIButton) {
let imagePicker: UIImagePickerController! = UIImagePickerController()
imagePicker.delegate = self
if (UIImagePickerController.isSourceTypeAvailable(.Camera)) {
if UIImagePickerController.availableCaptureModesForCameraDevice(.Front) != nil {
imagePicker.allowsEditing = false
imagePicker.sourceType = .Camera
imagePicker.cameraCaptureMode = .Photo
imagePicker.cameraDevice = .Front
imagePicker.modalPresentationStyle = .OverCurrentContext
presentViewController(imagePicker, animated: true, completion: {})
} else {
print("Camera not found")
}
} else {
print("Application cannot access the camera.")
}
}
@eviltofu
Copy link

eviltofu commented Jul 7, 2016

Why do you have a ! at the end of the constant declaration? It's not really required.

let imagePicker: UIImagePickerController! = UIImagePickerController()

Use guards instead of nested Ifs

guard UIImagePickerController.isSourceTypeAvailable(.Camera) else {
    print("Application cannot access the camera.")
    return
}    

guard UIImagePickerController.availableCaptureModesForCameraDevice(. Front) else {
    print("Front camera not found")
    return
}    

imagePicker.allowsEditing = false
imagePicker.sourceType = .Camera
imagePicker.cameraCaptureMode = .Photo
imagePicker.cameraDevice = .Front
imagePicker.modalPresentationStyle = .OverCurrentContext
presentViewController(imagePicker, animated: true, completion: {})

@vinamelody
Copy link
Author

Hmm @eviltofu, still having the warning
2016-07-07 16:47:52.130 Guestbook[3132:1758832] Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.

@IBAction func takePhoto(sender: UIButton) {

        let imagePicker: UIImagePickerController = UIImagePickerController()
        imagePicker.delegate = self

        guard UIImagePickerController.isSourceTypeAvailable(.Camera) else {
            print("Application cannot access the camera.")
            return
        }

        guard (UIImagePickerController.availableCaptureModesForCameraDevice(.Front) != nil) else {
            print("Camera not found")
            return
        }

        imagePicker.allowsEditing = false
        imagePicker.sourceType = .Camera
        imagePicker.cameraCaptureMode = .Photo
        imagePicker.cameraDevice = .Front
        imagePicker.modalPresentationStyle = .OverCurrentContext
        presentViewController(imagePicker, animated: true, completion: {})

    }

@echoz
Copy link

echoz commented Jul 8, 2016

Try presenting using something other than over current context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment