How to convert unmanaged to NSData?

后端 未结 2 1613
终归单人心
终归单人心 2021-01-12 19:06

I need to convert my Objective-C to Swift to get an image of a contact from the Address Book. But I get an error with for cast from CFData to NSData

2条回答
  •  情书的邮戳
    2021-01-12 19:39

    As explained in Working with Cocoa Data Types, you have to convert the unmanaged object to a memory managed object with takeUnretainedValue() or takeRetainedValue(). In your case

    if (ABPersonHasImageData(person)) {
        let imgData = ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatOriginalSize).takeRetainedValue()
        let image = UIImage(data: imgData)
    }
    

    because ABPersonCopyImageDataWithFormat() returns a (+1) retained value.

提交回复
热议问题