ios - AVAssetWriter 元数据日期问题

标签 ios swift avfoundation avassetwriter phphotolibrary

我有以下代码在 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

但是没有效果!

最佳答案

改变

    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

关于ios - AVAssetWriter 元数据日期问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57687646/

相关文章:

ios - 通信协议(protocol)/委托(delegate)上的 Swift 错误

ios - 如何从解析中获取数组的数组

iphone - 想要取回我拍摄的视频

iOS, swift : play background music and sound effects without delay

ios - 删除 Realm 对象会导致一个函数崩溃,而不是另一个函数

ios - UICollectionViewCell 获得对其父 UICollectionViewController 的访问权限

ios - Xcode 8 似乎错误地使用 Swift 3 更改了嵌入式框架函数签名

ios - AVFoundation -AVCaptureSession 仅在进入后台并返回断点时停止并开始运行

ios - 在 SwiftUI 中声明 Binding 属性有什么区别?

iOS:管理 REST 端点的简单方法