Leo
Leo

Reputation: 3143

Why is PHAsset creationDate different from exif metadata?

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?

Upvotes: 3

Views: 1349

Answers (1)

Sagar koyani
Sagar koyani

Reputation: 431

There are two kinds of metadata involved when you consider jpeg or other image file.

One is the file data. This is what the Finder shows. This tells you nothing about the contents of the file, just the File itself.

The problem with File metadata is that it can easily change as the file is moved from place to place or exported, e-mailed, uploaded etc.

Photographs have also got both Exif and IPTC metadata. The date and time that your camera snapped the Photograph is recorded in the Exif metadata. Regardless if what the file date says, this is the actual time recorded by the camera.

Photo applications like iPhoto, Aperture, Lightroom, Picasa, Photoshop etc get their date and time from the Exif metadata.

When you export from iPhoto to the Finder new file is created containing your Photo (and its Exif). The File date is - quite accurately - reported as the date of Export. However, the Photo Date doesn't change.

The problem is that the Finder doesn't work with Exif.

So, your photo has the correct date, and so does the file, but they are different things. To sort on the Photo date you'll need to use a photo app.

Final Words:- So if you want to get the exact date time when it was captured consider Exif Data. That is the real date and time.

Upvotes: 1

Related Questions