我正在尝试使用自定义相机。它处于婴儿期。我已经在线使用了几个教程,但由于最近的更改,代码已经过时了。我尽力调整它。但是我已经碰到了下面的墙。当我在应用程序中时,我可以拍摄照片并查看照片库,但是当我拍摄照片并选择它时,它不会出现,所以当我点击保存按钮时,应用程序崩溃并且控制台报告为零同时解开一个可选值。
有关纠正此问题并让图像显示在用户界面中以保存的任何帮助都将非常感激。这是代码:
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()
}
}
答案 0 :(得分:1)
并在故事板中重新连接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()
}
}