ios - Swift 3 相机无法保存

标签 ios swift uiimagepickercontroller

我正在尝试使用自定义相机。它还处于婴儿期。我在网上使用了几个教程,但由于最近的更改,代码已经过时了。我已经尽力调整它了。但是我遇到了下面的墙。当我在应用程序中时,我可以拍照并查看照片库,但是当我拍照并选择它时,它不会出现,所以当我点击保存按钮时,应用程序崩溃并且控制台报告 nil在展开一个可选值时。

任何有关更正此问题和让图像显示在 UI 中以供保存的帮助将不胜感激。这是代码:

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationBarDelegate {
    let imagePicker = UIImagePickerController()
    @IBOutlet weak var pickedImage: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        imagePicker.delegate = self as? UIImagePickerControllerDelegate & UINavigationControllerDelegate
    }

    @IBAction func camerabuttonaction(_ sender: UIButton) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self as? UIImagePickerControllerDelegate & UINavigationControllerDelegate
            imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
            imagePicker.allowsEditing = false
            self.present(imagePicker, animated: true, completion: nil)
        }
    }

    @IBAction func photolibraryaction(_ sender: UIButton) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self as? UIImagePickerControllerDelegate & UINavigationControllerDelegate
            imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary;
            imagePicker.allowsEditing = true
            self.present(imagePicker, animated: true, completion: nil)
        }
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]){
        var chosenImage = UIImage()
        chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
        pickedImage.contentMode = .scaleAspectFit
        pickedImage.image = chosenImage
        dismiss(animated:true, completion: nil)
    }

    func saveNotice(){
        let alertController = UIAlertController(title: "Image Saved!", message: "Your picture was successfully saved.", preferredStyle: .alert)
        let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        alertController.addAction(defaultAction)
        present(alertController, animated: true, completion: nil)
    }

    @IBAction func saveaction(_ sender: UIButton) {
        let imageData = UIImageJPEGRepresentation(pickedImage.image!, 0.6)
        let compressedJPEGImage = UIImage(data: imageData!)
        UIImageWriteToSavedPhotosAlbum(compressedJPEGImage!, nil, nil, nil)
        saveNotice()
    }
}

最佳答案

Please Check this in your code storyBoard if you replace Code

并重新连接 Storyboard 中的 IBAction 以避免任何崩溃

请将您的代码替换为以下代码保存并显示图像工作

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    var imagePicker = UIImagePickerController()
    @IBOutlet weak var pickedImage: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        imagePicker.delegate = self
    }

    @IBAction func camerabuttonaction(_ sender: UIButton) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
             imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
            imagePicker.allowsEditing = false
            self.present(imagePicker, animated: true, completion: nil)
        }
    }

    @IBAction func photolibraryaction(_ sender: UIButton) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
             imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary;
            imagePicker.allowsEditing = true
            self.present(imagePicker, animated: true, completion: nil)
        }
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]){
        var chosenImage = UIImage()
        chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
        pickedImage.contentMode = .scaleAspectFit
        pickedImage.image = chosenImage
        dismiss(animated:true, completion: nil)
    }

    func saveNotice(){
        let alertController = UIAlertController(title: "Image Saved!", message: "Your picture was successfully saved.", preferredStyle: .alert)
        let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        alertController.addAction(defaultAction)
        present(alertController, animated: true, completion: nil)
    }

    @IBAction func saveaction(_ sender: UIButton) {
        let imageData = UIImageJPEGRepresentation(pickedImage.image!, 0.6)
        let compressedJPEGImage = UIImage(data: imageData!)
        UIImageWriteToSavedPhotosAlbum(compressedJPEGImage!, nil, nil, nil)
        saveNotice()
    }
}

关于ios - Swift 3 相机无法保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45001314/

相关文章:

ios - flutter 构建 Xcode : Module 'firebase_analytics' not found

arrays - 遍历数组,如果值存在则返回 TRUE

swift - 链式动画扰乱了方向变化

ios - 具有不同覆盖层的单个 viewController 上的多个摄像头

ios - 允许编辑的 UIImagePickerController 会错误地裁剪图像,在顶部留下黑条

iphone - 没有背景图片的 UINavigationBar 样式

ios - 搜索二维数组以匹配两个维度 - Swift

ios - 自定义用户收到的通知

ios - "Invalid Swift Support - The SwiftSupport folder is missing"与 Xcode 7.3.1

iphone - 如何从照片库获取图像路径以及如何从iPhone照片库中检索图像?