Encoding of AVMetadataItem

2019-04-09 17:22发布

I have a AVMetadataItem which has fields encoded in CP1251 (Cyrillic). After reading item.stringValue I get garbage - incorrectly encoded string. I've tried converting that string to raw UTF8 and then creating a new string using the CP1251 encoding - no luck, result is nil. Tried taking the item.dataValue - no dice, it contains a raw list data (starting with bplist...).

Any ideas are very appreciated.

Thanks in advance.

4条回答
手持菜刀,她持情操
2楼-- · 2019-04-09 17:56

Swift 3.0 solution:

let data: Data = item.timedMetadata!.first!.stringValue.data(using: String.Encoding.isoLatin1, allowLossyConversion: true)!

let title: String = String(data: data as Data, encoding: String.Encoding.utf8)!
查看更多
小情绪 Triste *
3楼-- · 2019-04-09 18:05

I have AVMetadataItem extension for this:

extension AVMetadataItem {

    /// stringValue: ISO-8859-1 → UTF-8
    var utf8String: String? {
        guard let data = stringValue?.data(using: String.Encoding.isoLatin1, allowLossyConversion: true) else {
            return nil
        }
        return String(data: data as Data, encoding: String.Encoding.utf8)
    }
}
查看更多
成全新的幸福
4楼-- · 2019-04-09 18:12

Swift 2.0 solution:

let origTitleMeta: NSData = (<AVMetadataItem>.timedMetadata?.first?.stringValue?.dataUsingEncoding(NSISOLatin1StringEncoding, allowLossyConversion: true))!

let convertedTitleMeta: String = String(data: origMeta, encoding: NSUTF8StringEncoding)!
查看更多
疯言疯语
5楼-- · 2019-04-09 18:13

I've ended up using Mike Ash's NSPropertyListReader_binary1 and getting the raw data and then mangling it so that NSString would it it's encoding.

Horrible - but worked.

查看更多
登录 后发表回答