斯威夫特3;照片未保存到照片库

时间:2016-10-19 17:08:06

标签: swift camera save swift3 photo-gallery

此代码具有一个特殊的相机,其中包含一个嵌有标题的相机。因此,当照片保存到照片库时,标题就在照片上。相机效果很好,但照片没有保存。我只需要弄清楚如何将照片保存到照片库。我确实更改了plist以允许访问照片库和相机。

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")
}
}

1 个答案:

答案 0 :(得分:1)

这可能不是解决方案,但可能会更进一步为什么不保存图像(在评论的评论中来自rmaddy的提示)。要查看保存时是否有错误,请使用completionTargetcompletionSelector这样的参数(另请参阅docs):

添加completionSelector方法:

func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeMutableRawPointer) {
  guard error == nil else {
    print("Error saving image: \(error)")
    return
  }    
  print("Image saved successfully")
}

UIImageWriteToSavedPhotosAlbum更改为此

UIImageWriteToSavedPhotosAlbum(chosenImage, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)