问题
I have an image in the camera roll. I get the data representation if the image and md5 check sum it. We write the image to documents directory as img0.jpg Delete the original image and write the data representation back to the camera roll using ALAssetLibrary's writeImageDataToSavedPhotosAlbum.
Look in the photo album and the image looks to be the same. Read the newly saved image from the camera roll again.
Expected result - mathching md5. Actual result - different md5.
Save this file as well as img2.jpg
Hexdump the two and have a look at the difference:
Dans-Mac:Desktop dan$ hexdump img1.jpg | head
0000000 ff d8 ff e1 26 31 45 78 69 66 00 00 4d 4d 00 2a
0000010 00 00 00 08 00 05 01 1a 00 05 00 00 00 01 00 00
0000020 00 4a 01 1b 00 05 00 00 00 01 00 00 00 52 01 28
0000030 00 03 00 00 00 01 00 02 00 00 02 13 00 03 00 00
0000040 00 01 00 01 00 00 87 69 00 04 00 00 00 01 00 00
0000050 00 5a 00 00 00 b4 00 00 00 48 00 00 00 01 00 00
0000060 00 48 00 00 00 01 00 07 90 00 00 07 00 00 00 04
0000070 30 32 32 31 91 01 00 07 00 00 00 04 00 00 00 00
0000080 a0 00 00 07 00 00 00 04 30 31 30 30 a0 01 00 03
0000090 00 00 00 01 00 01 00 00 a0 02 00 04 00 00 00 01
Dans-Mac:Desktop dan$ hexdump img2.jpg | head
0000000 ff d8 ff e1 26 31 45 78 69 66 00 00 4d 4d 00 2a
0000010 00 00 00 08 00 05 01 1a 00 05 00 00 00 01 00 00
0000020 00 4a 01 1b 00 05 00 00 00 01 00 00 00 52 01 28
0000030 00 03 00 00 00 01 00 02 00 00 02 13 00 03 00 00
0000040 00 01 00 01 00 00 87 69 00 04 00 00 00 01 00 00
0000050 00 5a 00 00 00 b4 00 00 00 48 00 00 00 01 00 00
0000060 00 48 00 00 00 01 00 07 90 00 00 07 00 00 00 04
0000070 30 32 32 31 91 01 00 07 00 00 00 04 00 00 00 01
0000080 a0 00 00 07 00 00 00 04 30 31 30 30 a0 01 00 03
0000090 00 00 00 01 00 01 00 00 a0 02 00 04 00 00 00 01
If you have a look at line 0000070 the last character differ. This is happening for some images (most) but not all tested. In this particular case it is only one character difference in the header but other images tested faired worse with a lot of the data being different.
Is there any way at all on iOS to write actual image data to the photo library un-tainted? With what my application is doing I rely on the md5 check sum to be the same after I have saved the image to the photo library.
回答1:
I don't believe there is a reliable way to do this. When I was trying to use the gallery for saving PNGs with transparency, if the PNG were stored in batch from an email, they were converted to jpegs by iOS. Then I tried using iCloud with no success in preserving PNGs original format. If you're looking for something constant try extracting the id from the URL. Then at least you know you're working with the same thing even though the data might be changed.
回答2:
The Assets-Libray adds meta-data in some cases to the image. I observed, that for JPEG-Files the Assets-Library adds a JFIF-metadata-dictionary, if not already present. This will cause the file to grow in size and the MD5-Check to fail. There is no way to prevent this happening using the Assets-Library.
However, if you are only using JPEG's in your app, there might be a workaround to achieve what you want.
1) Before import extract the embedded thumbnail image using ImageIO and generate a checksum for this.
2) After import, read the first 128KB (or 64KB might also be ok) back from Assets-Library and extract again the thumbnail using imageIO. Then generate the checksum for the thumbnail. If the checksums match, it should be the same image.
来源:https://stackoverflow.com/questions/12623204/saving-image-using-writeimagedatatosavedphotosalbum-modifies-the-actual-image-da