当我单击导航栏上的“添加”按钮时,我需要将imageView的照片保存到图库中。我正在尝试保存它,但出现“线程1:信号SIGABRT”错误。也许有人知道,问题出在哪里?
class ViewControllerFilters2: UIViewController, UINavigationControllerDelegate {
@IBOutlet weak var imageView: UIImageView!
var filteredImage: UIImage?
var imagePicker: UIImagePickerController!
override func viewDidLoad() {
super.viewDidLoad()
if imageView.image == nil {
imageView.image = filteredImage!
}
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "✔", style: .plain, target: self, action: #selector(imagePickerController(_:didFinishPickingMediaWithInfo:)))
}
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage{
UIImageWriteToSavedPhotosAlbum(pickedImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
dismiss(animated: true, completion: nil)
}
}
@objc func image (_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
if let error = error {
// we got back an error!
let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
} else {
let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
}
}
答案 0 :(得分:0)
在刻度/添加按钮上被调用的方法似乎是错误的,目标应该是用于调用方法的uibarbutton项:
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "✔", style: .plain, target: self, action: #selector(imagePickerController(_:didFinishPickingMediaWithInfo:)))
更改用于保存图像的选择器,其中将包含用于将图像保存到相册的代码,例如:
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "✔", style: .plain, target: self, action: #selector(saveImage))
和保存图像的方法可能类似于:
@objc func saveImage() {
if let pickedImage = imageView.image {
UIImageWriteToSavedPhotosAlbum(pickedImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
}
此外,在imagePickerController的didFinish方法中,将图像分配到图像视图,以便单击添加后将保存新图像。
编辑
根据崩溃日志,在您的pList中为NSPhotoLibraryAddUsageDescription添加使用形式:
希望它会有所帮助..:)
答案 1 :(得分:0)
首先,我们必须在Project的.plist文件中进行权限设置
用于swift4
import UIKit
class photoPickerController: UIViewController,UINavigationControllerDelegate{
@IBOutlet weak var imageTake: UIImageView!
var imagePicker: UIImagePickerController!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func takePhoto(_ sender: UIButton) {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
@IBAction func saveToLibrary(_ sender: AnyObject) {
UIImageWriteToSavedPhotosAlbum(imageTake.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
}
extension photoPickerController : UIImagePickerControllerDelegate {
func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
if let error = error {
// we got back an error!
let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default))
present(alert, animated: true)
} else {
let alert = UIAlertController(title: "Saved!", message: "Image saved successfully", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default))
present(alert, animated: true)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imagePicker.dismiss(animated: true, completion: nil)
imageTake.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
}