-1

I've been digging around , but couldn't find any answer to this example.

I'm creating an App, that has User Data, such as name, profile picture and so on.

Every time the user makes changes and saves them, I update/save the changes to the web server to handle them.

Here's where my question enters.

Is there any way to check if the user changed his profile picture?

The user can access the camera roll and don't change the picture, just returns to the previous 'activity' without making changes, but he can also click to change the profile picture and choose the same picture that was already set, so how can I know that the picture is the same, so I can avoid the new upload to the server, of the same image?

Before uploading the image, I convert it to NSData, and I've checked the array of bytes generated and the first and last bytes, are most of the times the same, this because I tried to concatenate as a String to the end of the image name the last byte (as a string, for ex: ProfilePic89504e47), but I don't like this method at all.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
Ivan Cantarino
  • 3,058
  • 4
  • 34
  • 73

2 Answers2

1

You can just compare the two NSData objects using isEqualToData:. Meaning the one that you've already uploaded, and the new one that has just been selected from the camera.

Alternatively, you could calculate an MD5 hash (or similar) on the two NSData instances, and compare that.

Michael
  • 8,891
  • 3
  • 29
  • 42
-1

In general, if these are local images you should be able to store the image before they go to change their profile picture, and you can then compare that image to the one that is ultimately set via image1 == image2

You have to compare the base data, so from Here, you can do:

if let firstImage = UIImage(named: "firstImage") {
    let firstImageData = UIImagePNGRepresentation(firstImage)
    let compareImageData = UIImagePNGRepresentation(secondImage)

    if let empty = emptyData, compareTo = compareImageData {
        if empty.isEqualToData(compareTo) {
            // First image is the same as Second
        } else {
            // Second image is not equal to first
        }
    } else {
        // Creating NSData from Images failed
    }
}
Community
  • 1
  • 1
sschale
  • 5,168
  • 3
  • 29
  • 36
  • `image1 == image2` will only return `true` if the references are the same. It won't care about the values. – nhgrif Feb 24 '16 at 00:08
  • Your answer is no longer incorrect, but now your answer is no more than a copy & paste from an [existing answer](http://stackoverflow.com/a/34752898/2792531), in which case the correct course of action is not to answer this question, but instead mark this question as a duplicate of that question. – nhgrif Feb 24 '16 at 13:02