Avoiding to save image duplicates
My app allow users to choose imgs and associate them to some 开发者_JS百科text.
When the user choose an image from the photo roll, I copy it in the Documents directory.
As the user should choose the same img associating it to the same text or another one, how could I avoid to save duplicates of the same image?
I can't simply use fileExistsAtPath, because the name is generated on the fly.
Is there a way to know if the user is choosing an image already saved in the documents dir?
Have a look at Generate hash from UIImage, it shows two ways to compare two image with each other. The first one makes use of the md5 hash algorithm the other one uses NSData's isEqualToData in conjunction with the UIImagePNGRepresentation function.
However you could also compare file size and image dimensions first to avoid the more expensive approaches.
You could calculate a checksum and compare it with checksums of photos already in the documents directory.
精彩评论