2010-05-13 75 views
1

我有一个应用程序有一个表格视图,显示每一行中的联系人信息。我想使用联系人存储的图像(如果有可用的)作为单元格左侧的图像。如何在表格视图单元格中使用联系人的照片?

我在Apple的文档中发现了一些粗略的示例代码,但是地址簿引用了某种奇怪的数据类型(CFDataRef),它看起来不对应于表视图编程指南中引用的数据类型UIImage的)。

这似乎是一个非常基本的任务,但我似乎无法包围我的头。预先感谢您提供的任何帮助。

回答

3

您可以使用ABPersonHasImageData函数来检查联系人是否有图像。

下面是一个例子:

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { 
static NSString *id = @"id"; 
UITableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier: id ]; 

if(!cell) cell = [[ UITableViewCell alloc ] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: id ]; 

ABRecordRef record = ... 

UIImage *image; 
if(ABPersonHasImageData(record)) { 
    //record has an image 
    image = [ UIImage imageWithData: ABPersonCopyImageData(record) ]; 
} else { 
    image = [ UIImage imageNamed: @"silhouette.png" ]; //just an example 
} 

cell.imageView.image = image; 

// do other stuff with cell... 


return cell; 
} 

核心基础类型是免费电话与基金类型(CF == NS)

+0

真棒桥!正是我所需要的,并且像魅力一样工作。感谢您的快速帮助! – Andy 2010-05-13 03:45:33

+0

@安迪,不客气! – 2010-05-13 03:47:13

相关问题