This code features a special camera that features a camera with a title nested in it. So when the photo is saved to the photo gallery the title is on the photo. The camera works great but the photo is not being saved. I just need to figure out how to save the photos to the photo gallery. I did change the plist to allow access to photo gallery and camera.
import Foundation
import UIKit
class ViewController: UIViewController, OverlayViewControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var picker = UIImagePickerController()
@IBAction func letsDoThis(_ sender: AnyObject) {
if UIImagePickerController.availableCaptureModes(for: .rear) != nil {
picker = UIImagePickerController()
picker.allowsEditing = false
picker.sourceType = .camera
picker.cameraCaptureMode = .photo
picker.showsCameraControls = false
let overlayViewController = OverlayViewController(nibName:"OverlayView", bundle: nil)
let overlayView: OverlayView = overlayViewController.view as! OverlayView
overlayView.frame = self.picker.view.frame
overlayView.delegate = self
picker.modalPresentationStyle = .fullScreen
present(picker, animated: true, completion: {
self.picker.cameraOverlayView = overlayView
})
} else {
let alert = UIAlertController(title: "No Camera", message: "That's weird. No camera.", preferredStyle: .alert)
let okay = UIAlertAction(title: "Alright Then.", style: .default, handler: nil)
alert.addAction(okay)
present(alert, animated: true, completion: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
picker.dismiss(animated: true, completion: nil)
}
//MARK: Image Picker Controller Delegates
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
UIImageWriteToSavedPhotosAlbum(chosenImage, self,nil, nil)
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
print("Canceled!!")
}
func didCancel(overlayView: OverlayView) {
dismiss(animated: true, completion: nil)
print("dismissed!!")
}
func didShoot(overlayView: OverlayView) {
picker.takePicture()
overlayView.cameraLabel.text = "Shot Photo"
print("Shot Photo")
}
}
This might not the solution, but maybe it will take you further why the image is not saved (Hint from rmaddy in the comments of question). To see if there is an error while saving, use the completionTarget
and completionSelector
parameters like that (See also the docs):
Add the completionSelector
method:
func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeMutableRawPointer) {
guard error == nil else {
print("Error saving image: \(error)")
return
}
print("Image saved successfully")
}
Change UIImageWriteToSavedPhotosAlbum
to this
UIImageWriteToSavedPhotosAlbum(chosenImage, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)