简体   繁体   中英

PHAsset Creation date - check if UTC time or not?

When getting CreateionDate of PHAsset, the time returned is in UTC format. In order to display it correctly, so as to meet user's expectation that eg captured image of sunset was in the evening hours, not in the middle of night or day (depending on time-zone shift to UTC), the application needs to format the UTC date-Time by considering the timezone of the photo location.

I wrote a code, which handles this, and it works ok, but only for pictures which have geolocation and were taken with the iPhone camera, which is "aware" of time zone and so the UTC time it sets is correct. However if I open an image (asset) from PhotoLibrary which was taken with a camera not being "aware" of timezone concept (it only has a local time clock, which needs to be shifted when traveling to another timezone), than such a photo creation date refers to a non-UTC time (I guess that PhotoKit tries to derive UTC time of a photo - based on current for importing process local time shift against UTC-time - of the device used for importing).

If picture has geolocation, and the Creation Time is really UTC, I can derive timezone, and based on it I can format correctly UTC time to local time (of moment when picture was being taken). If the picture does not have geolocation, then I can't, despite the asset creation time is really UTC - User for some images may want to clear geolocation. Then I'm left with incorrect creation time (it is UTC but I don't know for which time zone I should format it)

The question is - how can I diffrentiate PHAsset's CreationDate - to know whether the value is, or is not time-zone "aware"? And, when knowing this, how to shift to proper timezone (to display local time of taking the picture), when there is no geolocation?

The iOS and Mac Photos Application displaying images from Photo Library seem to deal with this problem fine, as these display for these legacy photos just the EXIF date-time (what is ok, as long as the legacy camera had its local time correctly set to local time upon traveling, what can be assumed).

So, based on what does Photos App pick either [ready to display Exif Date] or [UTC Asset Creation Date]?

All pictures get a creation date of "now" based on the device's current idea of the current local time and the device's current timezone when the picture is taken or saved. Any device can have the wrong time or timezone so there's never any guarantee the picture's timestamp is accurate at all.

As for showing a picture's creation date in local time, you have only a few possible options. Use these in the following order to get the best result:

  • The picture has metadata (EXIF) that stores the date in local time. No conversion would be necessary.
  • The picture has metadata with the picture's GPS location. The coordinates can be used to work out the timezone of the picture and UTC timestamps can be adjusted.
  • None of that is available and the timestamp can only be shown in UTC or current local time (which may have nothing to do with where the picture was really taken).

Note that showing UTC time is always correct and unambiguous. Of course being able to show the picture's local time, when possible, can be more useful.

In short, first look at the picture's metadata for its local timestamp. Use that if it exists since that is in local time. See How to get original (EXIF?) creation date/time of PHAsset? for examples.

If you can't get the local creation time from the metadata, fallback to GPS adjustments or simply showing in UTC or current local time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM