Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 3;照片未保存到照片库_Swift_Camera_Save_Swift3_Photo Gallery - Fatal编程技术网

Swift 3;照片未保存到照片库

Swift 3;照片未保存到照片库,swift,camera,save,swift3,photo-gallery,Swift,Camera,Save,Swift3,Photo Gallery,此代码具有一个特殊的摄影机,该摄影机具有嵌套在其中的标题。因此,当照片保存到照片库时,标题就在照片上。相机工作得很好,但照片没有保存。我只是想知道如何将照片保存到照片库。我确实更改了plist以允许访问照片库和照相机 import Foundation import UIKit class ViewController: UIViewController, OverlayViewControllerDelegate, UIImagePickerControllerDelegate, U

此代码具有一个特殊的摄影机,该摄影机具有嵌套在其中的标题。因此,当照片保存到照片库时,标题就在照片上。相机工作得很好,但照片没有保存。我只是想知道如何将照片保存到照片库。我确实更改了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")
}
}

这可能不是解决方案,但可能会让您进一步了解为什么不保存图像(问题注释中来自rmaddy的提示)。要查看保存时是否有错误,请使用类似的
completionTarget
completionSelector
参数(另请参见):

添加
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)

听起来你是说调用
UIImageWriteToSavedPhotosAlbum
实际上并没有将图像保存到相册中,对吗?阅读
UIImageWriteToSavedPhotosAlbum
的文档,并利用传递给
nil
的额外参数,以便了解发生了什么。是的,这是正确的。您所说的额外周长是什么意思?他指的是
UIImageWriteToSavedPhotosAlbum
的最后两个参数,您将其设置为
nil
。你可以看看这里的文档。最后三个参数是可选的。但这不应该是你问题的原因我think@ronatory问题的关键在于,可以通过使用回调及其错误来确定问题的原因。没有错误,它只是不保存。这对我不起作用,但保存的照片会成功打印出问题所在的位置