I have an iPhone and a MacBook with the photo library synchronization enabled. iPhone has pictures that were copied to the MacBook's photo library.
This piece of code fetches an asset in the iOS app and prints the creationDate:
let allPhotosOptions = PHFetchOptions()
allPhotosOptions.sortDescriptors
= [NSSortDescriptor(key: "creationDate", ascending: true)]
let allPhotos = PHAsset.fetchAssets(with: self.allPhotosOptions)
let fetchResult = PHAsset.fetchAssets(in: allPhotos, options: nil)
let asset = fetchResult.lastObject!
let creationDate = asset.creationDate!
print("creationDate: \(dateFormatter.string(from: creationDate))")
With the date format yyyy-MM-dd HH:mm:ss.SSSS this prints:
creationDate: 2020-12-28 20:46:06.9940
Running the same code on macOS prints the different result on the same picture:
creationDate: 2020-12-28 20:46:09.1860
The date looks almost same but misses two seconds. I compared other photos that are identical between macOS and iOS and calculated the difference in seconds:
0.6877040863037109
0.5218453407287598
0.6767516136169434
0.32204413414001465
2.1924281120300293 (previously compared photo)
0.422029972076416
The dates are always different and the dates of iPhone photos are newer by random amount of millisecods. I sent previous photo from iOS to macOS as a file to compare with the same photo from the macOS library. When I print the exif data, it looks same:
$ exiftool IMG_1534_iOS.JPG
Create Date : 2020:12:28 20:46:09.186-08:00
$ exiftool IMG_1534_mac.HEIC
Create Date : 2020:12:28 20:46:09.186-08:00
As a last step, I tried to print exif data of the photo on iOS:
print("creationDate: \(dateFormatter.string(from: asset.creationDate!))")
let options = PHContentEditingInputRequestOptions()
asset.requestContentEditingInput(with: options) { input, _ in
guard let url = input?.fullSizeImageURL else { return }
guard let image = CIImage(contentsOf: url) else { return }
guard let exif = image.properties["{Exif}"] as? [String: Any] else { return }
print(exif["DateTimeOriginal"] ?? "")
print(exif["SubsecTimeDigitized"] ?? "")
}
This prints:
creationDate: 2020-12-28 20:46:06.9940
2020:12:28 20:46:09
186
Which means that on iOS PHAsset.creationDate differs from exif data that is stored in that PHAsset.
Why creationData of the same photo is different on macOS and iOS? And why creationData does not match exif's DateTimeOriginal on iOS?