ios - 避免在 Swift 中将重复的图像上传文件上传到 Parse.com

标签 ios swift parse-platform image-uploading

我在 ViewController 中创建了一个用户配置文件编辑页面。当用户仅编辑文本字段并点击保存按钮时,即使图片相同,它仍会再次上传。我想检查图片是否相同,跳过当前图片的上传。解析还使用相同的图像文件名,因此按名称检查将不起作用,我不知道还能使用什么。有任何想法吗? 这是我的代码:

    let imageUploadData = UIImageJPEGRepresentation(imageUpload.image!, 1)
        if (imageUploadData != nil) {
                //Here what I tried:
            if (currentImage.image != imageUploadData) {
                print("Image will be uploaded")
                let imageFileObject = PFFile(data: imageUploadData!)
                myUser.setObject(imageFileObject!, forKey: "license_image")
            }else {
                 print("Image is the same, skipping upload")
            }
        }
        myUser.saveInBackgroundWithBlock {(success: Bool, error:NSError?) -> Void in
        self.clearAllNotice()  //Clear activity indicator

        if (error != nil) {
            print("...")
        }
        if (success) {
            print("Saving")
        }

我尝试了什么,在下面的评论中提到

let userImageFile = PFUser.currentUser()?.objectForKey("license_image") as! PFFile
        userImageFile.getDataInBackgroundWithBlock {(imageData: NSData?, error: NSError?) -> Void in
            if (error == nil) {
                let image = UIImage(data:imageData!)
                if image == self.imageUpload.image {
                    print("image is the same")
                }
                else {
                    print("image not the same")
                }
            }
        }

我也试过这个:

let userImageFile = PFUser.currentUser()?.objectForKey("license_image") as! PFFile
        userImageFile.getDataInBackgroundWithBlock {(imageData: NSData?, error: NSError?) -> Void in
            if (error == nil) {
                let image = UIImage(data:imageData!)
                let imageFileObject = PFFile(data: imageUploadData!)
                if ((image?.isEqual(imageFileObject)) != nil){
                    print("image is the same")
                }
                else {
                    print("image not the same")
                }
            }
        }

没有运气

最佳答案

只需获取他们已经保存的图像数据,并将其与即将保存的图像数据进行比较,如果相同则不允许他们继续或任何您的用例.

关于ios - 避免在 Swift 中将重复的图像上传文件上传到 Parse.com,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36391237/

相关文章:

ios - 在登录页面全屏播放视频

ios - block 定义在整个项目中可用,没有重复的符号错误

ios - @autoreleasepool出错

swift - 当输入数组保存在变量中时,调用 Swift 中的泛型函数无法编译

javascript - 在 React Native 中——是否可以在通过 Prop 接收到的动画值上添加一个监听器?

swift - WKWebView 在 iOS 9.0 中无法快速显示网站

ios - 协议(protocol)函数位于单独的文件中

ios - 尝试将 Parse 中的一行数据提取到 Swift 中的数组中

javascript - 解析查找查询后组件的 Reactjs 渲染

parse-platform - 在保存前解析检查原始对象