AVAssetWriter 元数据日期问题
Posted
技术标签:
【中文标题】AVAssetWriter 元数据日期问题【英文标题】:AVAssetWriter metadata Date issue 【发布时间】:2019-08-28 08:03:55 【问题描述】:我有以下代码在 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
当使用 phphotoLibrary 导出到照片库时,它会在照片库中显示 1970 年 1 月 1 日或有时 469231 年 9 月 2 日等日期作为创建日期。我做错了什么?
我还尝试了以下方法:
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)
然后简单地使用:
let creationDateMetaData = AVMutableMetadataItem()
creationDateMetaData.keySpace = AVMetadataKeySpace.common
creationDateMetaData.key = AVMetadataKey.commonKeyCreationDate as NSCopying & NSObjectProtocol
creationDateMetaData.value = Date().iso8601() as NSCopying & NSObjectProtocol
但是没有效果!
【问题讨论】:
【参考方案1】:改变
let creationDateMetaData = AVMutableMetadataItem()
creationDateMetaData.keySpace = AVMetadataKeySpace.common
creationDateMetaData.key = AVMetadataKey.commonKeyCreationDate as NSCopying & NSObjectProtocol
creationDateMetaData.value = Date().iso8601() as NSCopying & NSObjectProtocol
到
let creationDateMetaData = AVMutableMetadataItem()
creationDateMetaData.keySpace = .common
creationDateMetaData.key = AVMetadataKey.commonKeyCreationDate as NSString
creationDateMetaData.value = Date() as NSDate
【讨论】:
似乎在 Mac 上与照片应用程序一起工作。以上是关于AVAssetWriter 元数据日期问题的主要内容,如果未能解决你的问题,请参考以下文章
NSFileManager 是不是将修改日期等元数据与文件一起复制?