Ios 如何设置CNMutableContact的“thumbnailImageData”的裁剪信息
我使用Ios 如何设置CNMutableContact的“thumbnailImageData”的裁剪信息,ios,swift3,cncontact,Ios,Swift3,Cncontact,我使用CNMutableContact创建/更新联系人。 我可以通过imageData属性设置新图像,但我需要设置用于创建缩略图的自定义裁剪信息。属性thumbnailImageData为只读 代码: 如何添加自定义缩略图图像裁剪?在iOS中似乎无法设置缩略图。但是,根据定义,图像的缩略图是被裁剪为更小尺寸的同一图像。因此,在保存联系人时,iOS将根据联系人上的图像数据集自动生成缩略图 如果您想为缩略图和实际联系人图像设置不同的图像,iOS将不允许您这样做。 我遇到的问题: 在用户的联系人中添加
CNMutableContact
创建/更新联系人。
我可以通过imageData
属性设置新图像,但我需要设置用于创建缩略图的自定义裁剪信息。属性thumbnailImageData
为只读
代码:
如何添加自定义缩略图图像裁剪?在iOS中似乎无法设置缩略图。但是,根据定义,图像的缩略图是被裁剪为更小尺寸的同一图像。因此,在保存联系人时,iOS将根据联系人上的图像数据集自动生成缩略图 如果您想为缩略图和实际联系人图像设置不同的图像,iOS将不允许您这样做。 我遇到的问题: 在用户的联系人中添加新联系人(
CNMutableContact
reference)之前,我想向用户显示联系人。我可以使用imageData
设置新联系人的图像。但是,当使用CNContactViewController
显示此新联系人时,图像不会按照缩略图进行裁剪。显示的缩略图像看起来非常怪异和缩放。如何解决这个问题
解决方案:
这是因为CNMutableContact
对象上的thumbnailImageData
属性为nil。开发人员无法设置此属性。此属性只能由iOS内部设置,并在保存联系人时由iOS自动生成
因此,在显示CNMutableContact
对象之前,您应该将其保存到用户联系人,以启动自动缩略图生成,然后立即删除联系人
CNMutableContact上的以下扩展描述了如何实现这一点
extension CNMutableContact {
func generateThumbnailImage() {
if self.thumbnailImageData != nil {
return
}
// contact.thumbnailImageData is nil
// First save the contact for the thumbnail to be generated
let saveRequest = CNSaveRequest()
saveRequest.add(self, toContainerWithIdentifier: nil)
do {
try CNContactStore().execute(saveRequest)
} catch let error {
print("Error occurred while saving the request \(error)")
}
// self.thumbnailImageData is not nil. Contact Store will generate the thumbnail for this contact with the imageData provided.
// Now delete the contact
let deleteRequest = CNSaveRequest()
deleteRequest.delete(self)
do {
try CNContactStore().execute(deleteRequest)
} catch let error {
print("Error occurred while deleting the request \(error)")
}
// The contact is removed from the Contact Store
// However, the contact.thumbnailImageData is not nil anymore. Contacts Store has generated the thumbnail automatically with the imageData provided.
}
}
你好你找到解决办法了吗?遇到了同样的问题,好像这是不可能的。谢谢,@iRavi,但我不明白,怎样才能进球。你能分享代码吗?这对我来说仍然是真实的。@Igor添加了代码。请注意,此解决方案仅用于使用iOS生成缩略图信息,而不是设置缩略图信息。希望这对你有所帮助。但是,我认为,如果我更改imageData,将生成新的thumbnailImageData。我需要存储联系人的图像并从另一个区域创建thumbnailImageData,而不是自动生成器。你找到办法了吗?很抱歉,如果我仍然误解了您的意思。@I或者我认为我们需要一个不同的用例,因此会产生混淆。只是澄清一下,目前无法在iOS中为联系人设置thumbnailImageData。编辑我的答案来解释我的用例和解决方案。我有另一个案例。我只需要以编程方式更改缩略图的裁剪位置(用户如何通过嵌入联系人应用程序更改它)。但是,我认为这是不可能的。
extension CNMutableContact {
func generateThumbnailImage() {
if self.thumbnailImageData != nil {
return
}
// contact.thumbnailImageData is nil
// First save the contact for the thumbnail to be generated
let saveRequest = CNSaveRequest()
saveRequest.add(self, toContainerWithIdentifier: nil)
do {
try CNContactStore().execute(saveRequest)
} catch let error {
print("Error occurred while saving the request \(error)")
}
// self.thumbnailImageData is not nil. Contact Store will generate the thumbnail for this contact with the imageData provided.
// Now delete the contact
let deleteRequest = CNSaveRequest()
deleteRequest.delete(self)
do {
try CNContactStore().execute(deleteRequest)
} catch let error {
print("Error occurred while deleting the request \(error)")
}
// The contact is removed from the Contact Store
// However, the contact.thumbnailImageData is not nil anymore. Contacts Store has generated the thumbnail automatically with the imageData provided.
}
}