Created
July 7, 2016 08:07
-
-
Save vinamelody/015c24ac72b73f11edadf1481905a112 to your computer and use it in GitHub Desktop.
Swift: Take photo button action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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.") | |
} | |
} |
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: {})
}
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
Why do you have a ! at the end of the constant declaration? It's not really required.
Use guards instead of nested Ifs