首页 > 解决方案 > 斯威夫特:检查图片网址是否已更改

问题描述

我有一个视图,它显示来自 collectionView 的选定对象(产品),并让用户可以更改该产品。在返回 collectionViewController 之前,我想检查是否有任何更改,并询问用户是否要保存。

Product 包含 imageView 的 url?.absoluteString。我与 Equatable 进行比较,

if product1 != product2 { 
   ask if save or not 
}

最大的问题是,当我创建 absolutString 时,图像会上传到 Firebase,这会花费不必要的时间。如果我不想保存更改,为时已晚,因为我已经更改了 firebase 中的图像。

// getting the url String

func uploadImage(_ image: UIImage, completion: @escaping (_ url: URL?) -> ()) {
        let storageRef = Storage.storage().reference().child("myImage.jpeg")
        let imageData = imageHolder.imageView.image?.jpegData(compressionQuality: 0.5)
        let metaData = StorageMetadata()
        metaData.contentType = "image/jpeg"
        storageRef.putData(imageData!, metadata: metaData) { (metadata, error) in
            if error == nil {
                print("success")
                storageRef.downloadURL(completion: { (url, error) in
                    completion(url!)
                })
            } else {
                print("error in save image")
                completion(nil)
            }
        }
    }


// comparing the two objects

func checkEditedProduct() -> Bool {

        var productsAreSame = true

        uploadImage(imageHolder.imageView.image!) { (url) in
            let product = Product(name: self.productName.text.text!,
                                  barCode: self.productGANText.text!,
                                  sellPrice: Int(self.productPrice.text.text!) ?? 0,
                                  buyPrice: Int(self.productWorth.text.text!) ?? 0,
                                  picture: url?.absoluteString,
                                  numberOfItemsInStock: Int(self.productInStock.text.text!) ?? 0,
                                  stockKeepingUnit: self.productSKU.text.text)
            self.compareProduct.append(product)
        }

        if compareProduct.count == 2 {
            if compareProduct[0] != compareProduct[1] {
                productsAreSame = false
            }
        }

        return productsAreSame
    }

我不知道是否有一种方法可以检查 imageView 中的图像是否已被篡改,而无需对 firebase 数据库或存储进行任何调用

标签: swiftimageviewcompareequatable

解决方案


推荐阅读