我正在从图库中拾取图像,但是我的代码未拾取图像,并且单击图像到图片后出现错误

时间:2019-05-06 10:23:49

标签: swift

在此错误下没有选择图像。.在Swift 4.2中(并将其保存在领域中)给出了错误(错误域= PlugInInKit代码= 13“查询已取消” UserInfo = {NSLocalizedDescription =查询已取消}) 下面是完整的代码

import UIKit
import RealmSwift
import Photos

class PhotoViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    @IBOutlet weak var CollectionView: UICollectionView!
    var photo = [Pic]()
    let realm = try! Realm()
    var selectedValue : Note?{
        didSet{
            loadPic()
        }
    }
    let imagePickerController = UIImagePickerController()
    override func viewDidLoad() {
        super.viewDidLoad()
        CollectionView.delegate = self
        CollectionView.dataSource = self
        imagePickerController.delegate = self

    }
    //    override func viewWillAppear(_ animated: Bool) {
    //        title = selectedValue?.title ?? "no note found"
    //    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return photo.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Photos", for: indexPath) as! PhotoCollectionViewCell
        let noteItem = photo[indexPath.row]
        if let noteImage = UIImage(data: noteItem.conFile! as Data){
            cell.MyLibrary.image = noteImage
        }
        cell.MyLabel.text = noteItem.name
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath.item)
    }
    @IBAction func AddPhoto(_ sender: UIBarButtonItem) {
        let alert = UIAlertController(title: "Add New Photo To Collection", message: "Choose Any Photo", preferredStyle: .alert)
        let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        let photo = UIAlertAction(title: "Gallery", style: .default) { (photo) in
            self.imagePickerController.sourceType = .photoLibrary
            self.present(self.imagePickerController, animated: true, completion: nil )
        }
        let camera = UIAlertAction(title: "Camera", style: .default) { (camera) in
            if UIImagePickerController.isSourceTypeAvailable(.camera){
                self.imagePickerController.sourceType = .camera
                self.present(self.imagePickerController, animated: true, completion: nil)
            }
            else{
                print("camera is not available")
            }
        }
        alert.addAction(photo)
        alert.addAction(camera)
        alert.addAction(cancel)
        present(alert, animated: true, completion: nil)
    }
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard let image = info[.originalImage] as? UIImage else{
            fatalError("Image is not Picked\(info)")
        }
        picker.dismiss(animated: true, completion: nil)
        self.createNoteItem(with:image)
    }
    func createNoteItem(with image : UIImage){
        func save() {
            do {
                try self.realm.write {
                    let noteItem = Pic()
                    let pngdata = image.pngData()
                    noteItem.conFile = pngdata as NSData?
                }
            } catch {
                print("There was an error appending notes \(error)")
            }
        }
        let alert = UIAlertController(title: "Details", message: "Enter The Name", preferredStyle: .alert)
        alert.addTextField{(textfield:UITextField) in
            textfield.placeholder = "Enter Name"
        }
        alert.addAction(UIAlertAction(title: "add", style: .default, handler: { (action : UIAlertAction) in
            func save() {
                do {
                    try self.realm.write {
                        let  noteItem = Pic()
                        let nameField = alert.textFields?.first
                        if nameField?.text != "" {
                        noteItem.name = nameField?.text! ?? "Unknow Photo"
                        }
                    }
                } catch {
                    print("There was an error appending notes \(error)")
                }
            }
            }))
    }

0 个答案:

没有答案