嗨,我正在创建自定义地址簿,我只想让拥有电子邮件地址的用户一切正常,但是当我尝试在表格 View 中显示图像时,它给了我EXCBAD错误
。我有这样的代码
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
for (CFIndex i = 0; i < CFArrayGetCount(people); i++) {
ABRecordRef person = CFArrayGetValueAtIndex(people, i);
ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
for (CFIndex j=0; j < ABMultiValueGetCount(emails); j++) {
UIImage *img;
if(ABPersonHasImageData(person))
{
// img = [UIImage imageWithData:(NSData *)ABPersonCopyImageDataWithFormat(person,kABPersonImageFormatThumbnail)];
img = [UIImage imageWithData:(NSData *)ABPersonCopyImageData(person)];
// if(img != nil)
// {
// [arrImage addObject:img];
// }
// else
// {
img = [UIImage imageNamed:@"icon-9.png"];
[arrImage addObject:img];
// }
}else
{
img = [UIImage imageNamed:@"icon-9.png"];
[arrImage addObject:img];
}
NSString* email = (NSString*)ABMultiValueCopyValueAtIndex(emails, j);
// [arrImage addObject:img];
[arrEmail addObject:email];
[img release];
[email release];
}
CFRelease(emails);
}
CFRelease(addressBook);
CFRelease(people);
在上面的代码中,我正在获取图像并将图像存储在 NSMutableArray 中,它将被完美存储。但是当我要在下面的代码中显示图像时
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UIImageView* imagePhoto;
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableFriends dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
imagePhoto= [[[UIImageView alloc] initWithFrame:CGRectMake(43.0,10.0,40, 40)] autorelease];
if ([arrEmail count] != 0)
{
imagePhoto.image = [arrImage objectAtIndex:indexPath.row];
[cell.contentView addSubview:imagePhoto];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[arrEmail objectAtIndex:indexPath.row]];
cell.detailTextLabel.textColor = [UIColor whiteColor];
}
return cell;
}
当从这里调用图像时,它工作得很好 img = [UIImage imageNamed:@"icon.png"];
但是当从地址簿调用图像时,它会给我错误 EXCBAD Error
该代码有什么问题?如何将图像存储在 NSMutabeArray 中并显示图像?
嘿帮助我任何人都可以帮助我或建议我实现像 Google Latitude 在这里你可以看到在从iPhone地址簿添加 friend 中我只想实现一些像这个应用程序的功能我的所有代码都工作良好只是我无法从地址簿获取图像请帮助我....
最佳答案
我已经将图像存储在 NSMutableDictionary 的对象中,如下所示
personrecord 是
ABRecordRef的实例
假设您正在 ARC 模式下构建项目。
NSData *imgData = (__bridge_transfer NSData *) ABPersonCopyImageDataWithFormat(personrecord, kABPersonImageFormatThumbnail); if (imgData) { UIImage *personImage = [UIImage imageWithData:imgData]; imgData = nil; [dict setValue:personImage forKey:@"UserImage"]; } else { UIImage *personImage = [UIImage imageNamed:@"defalt_contact_img.png"]; [dict setValue:personImage forKey:@"UserImage"]; }
您可以操作代码以满足您的要求,我使用字典,您使用数组,因此您可以使用数组的 addObject 方法来存储图像,
我总是更喜欢从具有多个字典的可变数组中获取地址簿中的记录,这样记录的操作就变得很容易。
关于iphone - 从地址簿获取图像并将其存储到 NSmutablearray 但在显示它时在 iphone sdk 中给出 `EXCBAD Error` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10043420/