当我存钱的时候,正常的取钱和存钱。
当我尝试更新任何东西时,图像变为零并显示空白图像
当我执行“执行相机操作”时,剩余数据将变为零,只需打开相机,剩余数据将变为零。
主要的问题是为什么图像在更新时变为零,而其余的都保持不变。只有图像将为空。
我能做些什么来解决这个问题?

    //Navigation and ImagePicekrDelegate are for camera
    var imagePicker: UIImagePickerController!
    @IBOutlet weak var showImage: UIImageView!

    var itemImageVar :UIImage? {
        didSet{

        }

    }

    override func viewWillAppear(_ animated: Bool) {
        showImage.image = itemImageVar
    }

    //TODO: Saving image Here
    var imageData:Data?

    //Select Picture
    @IBAction func choosePhotAction(_ sender: UIButton) {
        imagePicker =  UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .photoLibrary  //This is for opening photo library
        present(imagePicker, animated: true, completion: nil)
    }

   @IBAction func takePhotoActionw(_ sender: UIButton) {
        imagePicker =  UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .camera
        present(imagePicker, animated: true, completion: nil)
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let imageViewer = info[UIImagePickerController.InfoKey.originalImage] {
            imagePicker.dismiss(animated: true,completion: nil)
            showImage.image = imageViewer as! UIImage
            saveImage(image: imageViewer as! UIImage)
        }
    }

    func saveImage(image : UIImage) {
        imageData = (image as! UIImage).jpegData(compressionQuality: 100)
        print(imageData)
    }

    @IBAction func saveBtnAction(_ sender: UIButton) {
        let newArray = Items()//Item is realm Data Model
        newArray.itemName = itemName.text!


 if let data = imageData as Data?  {
                                newArray.itemImage = data
                            }


        newArray.dateWise = Date()

        //MARK: Realm data
        //Creating/saving data here
        if saveFlag == true {
            //This will go to Create data and save
            self.saveItems(item: newArray)
        } else {
            newArray.id = idVar!
            self.updateItems(item: newArray)
        }

        self.navigationController?.popViewController(animated: true)
   }

    func saveItems(item: Items) {
        do {
           try realm.write { //Save/Create
               realm.add(item)
            }
        } catch {
           print("Error in saving \(error)")
        }
    }

   func updateItems(item: Items) {
       do {
           try realm.write {//Save/Create
              realm.add(item, update: .modified)
           }
       } catch {
           print("Error in saving \(error)")
       }
   }

   //Read the data after saving like displying again
   func displayItems() {
      itemArray = realm.objects(Items.self)
   }

显示视图控制器:-
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = self.tableView.dequeueReusableCell(withIdentifier: "ItemDispplayCell", for: indexPath) as! ItemDispplayCell

            if let cellItems = itemArray?[indexPath.row] {

                if let data = item.itemImage as Data? {
                                                 cell.circleImag.image = UIImage(data: data)
                                         }


            }else {
                cell.itemName.text = "No Items Added"
            }


            return cell
           }

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 if let item = itemArray?[indexPath.row] {

            if let data = item.itemImage as Data?  {
                vc.imageVar = UIImage(data: data)
                                          }

}

    func displayItems() {

              itemArray  = realm.objects(Items.self)


            tableView.reloadData()

           }

最佳答案

发生崩溃是因为您正在强制展开可选值。有更好的方法来处理这样的案件。例如使用if let

if let data = cellItems.itemImage as? Data {
    cell.circleImag.image = UIImage(data: data))
}

如果你想从UIImage中获取Data,你不应该使用imageData as Data?,而是:
let data = image.pngData()

10-06 02:55