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

Posted

技术标签:

【中文标题】斯威夫特 3;照片未保存到照片库【英文标题】:Swift 3; photo is not saving to photo gallery 【发布时间】:2016-10-19 17:08:06 【问题描述】:

此代码包含一个特殊的摄像头,其中包含一个嵌套有标题的摄像头。因此,当照片保存到照片库时,标题就在照片上。相机很好用,但照片没有保存。我只需要弄清楚如何将照片保存到照片库。我确实更改了 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")


【问题讨论】:

听起来您是在说对UIImageWriteToSavedPhotosAlbum 的调用实际上并没有将图像保存到相册,对吗?阅读UIImageWriteToSavedPhotosAlbum 的文档,并利用您传递给nil 的额外参数,以便了解发生了什么。 是的,这是正确的。额外的周长是什么意思? 他的意思是UIImageWriteToSavedPhotosAlbum的最后两个参数,你设置为nil。您可以在文档developer.apple.com/reference/uikit/… 中查看此处。最后三个参数是可选的。但这不应该是你的问题的原因我认为 @ronatory 重点是可以通过回调及其错误来确定问题的原因。 没有错误只是不保存 【参考方案1】:

这可能不是解决方案,但可能会让您更进一步为什么不保存图像(来自问题 cmets 中的 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)

【讨论】:

这对我不起作用,但保存的照片在问题所在的地方打印成功

以上是关于斯威夫特 3;照片未保存到照片库的主要内容,如果未能解决你的问题,请参考以下文章

照片和视频未保存在我的自定义相册中

IOS13:选择保存照片后未调用 UIActivityViewController.completionWithItemsHandler

钉钉提交工单存草稿照片会有吗

怎样把微信照片存到相册里

微信拍照不保存相册怎么设置

将照片保存到iOS相册,不要全部保存