本文主要是介绍设置、读取联络人头像 ---- save UIImage to ABAddressBook,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
其中设置和读取联系人头像有两种方法
1、通过系统方法设置
其中要有2个参数 1、UIImage 2、联系人电话号码
extern "C" ABRecordRef ABCFindPersonMatchingPhoneNumber(ABAddressBookRef addressBook,NSString *phoneNumber,int, int);
ABAddressBookRef addressbook = ABAddressBookCreate();
ABRecordRef ref1 = ABCFindPersonMatchingPhoneNumber(addressbook, pn, 0, 0);
int record = ABRecordGetRecordID(ref1);//获取联络人的recordId
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressbook, record);//获取联络人的ABRecordRef
/
设置联络人图片
UIImage * img = [UIImage imageNamed:@"image.png"];
NSData *dataRef = UIImagePNGRepresentation(img);
ABPersonSetImageData(person, (CFDataRef)dataRef, nil);
ABAddressBookAddRecord(addressbook, person, nil);
ABAddressBookSave(addressbook, nil);
CFRelease(addressbook);//create、copy、retrain对应的release
读取系统联络人图片
CFDataRef dataRef = ABPersonCopyImageData(person);
UIImage *image = [UIImage imageWithData:(NSData *)dataRef];//这个就是我们要获取的Image的值
[dataRef release];
注意:用系统方法插入的操作在数据库中保存的时候只会保存缩略图的形式
相当于在数据库中的格式format为0 fromat(分为3中 0为缩略图 1、大号缩略图 2、源图)
///
///
使用读取数据库的方法来读取 (这里就只说读取,具体的插入也类似)
#define DBNAME "/var/mobile/Library/AddressBook/AddressBookImages.sqlitedb"
-(UIImage *) getSystemImage:(int)record_id formatImg:(int) format
{
sqlite3 *m_db;
UIImage *image = nil;
int m_errno;
NSData* dataObj = nil;
m_errno=sqlite3_open(DBNAME,&m_db);
if (m_db==NULL) {
return nil;
}
int len;
char *next;
sqlite3_stmt *st;
char sql[256];
sprintf(sql,"select data from ABImage where record_id = %d and format = %d",record_id, format);
len = strlen(sql);
m_errno = sqlite3_prepare(m_db, (const char *)sql, len, &st, (const char **)&next);
if (m_errno != SQLITE_OK)
{
return nil;
}
m_errno = sqlite3_step(st);
if(m_errno == SQLITE_ROW)
{
int length = sqlite3_column_bytes(st, 0);
const unsigned char*pData = sqlite3_column_text(st, 0);
dataObj = [NSData dataWithBytes:pData length:length];
}
image = [UIImage imageWithData:dataObj];
sqlite3_finalize(st);
sqlite3_close(m_db);
return image;
}
这篇关于设置、读取联络人头像 ---- save UIImage to ABAddressBook的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!