[英]AVAssetWriter metadata Date issue
I have the following code to write the date metadata in AVAssetWriter which is going all wrong in iOS 13. Need to know if this is an iOS 13 bug or I am doing something wrong. 我有以下代码在AVAssetWriter中写入日期元数据,这在iOS 13中会出错。需要知道这是否是iOS 13错误或我做错了什么。
private var metadataItems:[AVMetadataItem]? {
get {
let locale = Locale.current
var metadata:[AVMetadataItem] = []
let date = Date()
let calendar = Calendar.current
let year = calendar.component(.year, from: date)
let month = calendar.component(.month, from: date)
let day = calendar.component(.day, from: date)
let hour = calendar.component(.hour, from: date)
let minute = calendar.component(.minute, from: date)
let second = calendar.component(.second, from: date)
let timezone = TimeZone.current
let timezoneStr = timezone.abbreviation()
let creationDateMetaData = AVMutableMetadataItem()
creationDateMetaData.keySpace = AVMetadataKeySpace.common
creationDateMetaData.key = AVMetadataKey.commonKeyCreationDate as NSCopying & NSObjectProtocol
creationDateMetaData.locale = locale
creationDateMetaData.value = String(format:"%04ld-%02ld-%02ld %02ld:%02ld:%02ld %@",year, month, day, hour, minute, second, timezoneStr!) as NSCopying & NSObjectProtocol
metadata.append(creationDateMetaData)
return metadata
}
It shows dates like 1 Jan 1970 or sometimes 2 Sept 469231 as creation date in Photo Library when exported to photo library using PHPhotoLibrary. 当使用PHPhotoLibrary导出到照片库时,它在照片库中显示的日期是1970年1月1日,有时是469231年9月2日。 What am I doing wrong? 我究竟做错了什么?
I also tried the following: 我还尝试了以下方法:
extension Date {
static let dateFormatter: DateFormatter = iso8601DateFormatter()
fileprivate static func iso8601DateFormatter() -> DateFormatter {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
return formatter
}
// http://nshipster.com/nsformatter/
// http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
public func iso8601() -> String {
return Date.iso8601DateFormatter().string(from: self)
}
}
And then simply using: 然后只需使用:
let creationDateMetaData = AVMutableMetadataItem()
creationDateMetaData.keySpace = AVMetadataKeySpace.common
creationDateMetaData.key = AVMetadataKey.commonKeyCreationDate as NSCopying & NSObjectProtocol
creationDateMetaData.value = Date().iso8601() as NSCopying & NSObjectProtocol
But no effect! 但是没有效果!
Change 更改
let creationDateMetaData = AVMutableMetadataItem()
creationDateMetaData.keySpace = AVMetadataKeySpace.common
creationDateMetaData.key = AVMetadataKey.commonKeyCreationDate as NSCopying & NSObjectProtocol
creationDateMetaData.value = Date().iso8601() as NSCopying & NSObjectProtocol
To 至
let creationDateMetaData = AVMutableMetadataItem()
creationDateMetaData.keySpace = .common
creationDateMetaData.key = AVMetadataKey.commonKeyCreationDate as NSString
creationDateMetaData.value = Date() as NSDate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.