[ios] ContactsUI에서 연락처의 이미지를 업데이트하는 방법은 무엇인가요?

먼저, 새로운 이미지를 포함하는 CNContact를 만듭니다. 그런 다음 CNContactStore를 사용하여 이미지가 포함된 CNContact를 저장합니다. 저장을 완료하면 이제 해당 연락처의 이미지가 업데이트됩니다.

아래는 이미지를 업데이트하는 간단한 예제 코드입니다.

import Contacts
import ContactsUI

func updateContactImage(with image: UIImage, for contact: CNContact) {
    let store = CNContactStore()
    
    // Create a mutable copy of the contact
    let mutableContact = contact.mutableCopy() as! CNMutableContact
    
    // Assign the new image to the mutable contact
    mutableContact.imageData = image.jpegData(compressionQuality: 1.0)
    
    do {
        let saveRequest = CNSaveRequest()
        saveRequest.update(mutableContact)
        try store.execute(saveRequest)
        // 이미지가 성공적으로 업데이트되었습니다.
    } catch {
        // 이미지 업데이트 중 오류가 발생했습니다.
    }
}

위의 코드는 ContactsUI에서 CNContact의 이미지를 업데이트하는 방법을 보여줍니다. 사용자가 연락처의 이미지를 업데이트하고자 할 때, 앱에서 이 코드를 호출하여 이미지를 업데이트할 수 있습니다.

더 자세한 내용은 Apple의 공식 문서를 참조하십시오.