iphone - 从地址簿获取图像并将其存储到 NSmutablearray 但在显示它时在 iphone sdk 中给出 `EXCBAD Error` ?

标签 iphone abaddressbook abpeoplepickerview

嗨,我正在创建自定义地址簿,我只想让拥有电子邮件地址的用户一切正常,但是当我尝试在表格 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/

相关文章:

iphone - 如何使用 ABPeoplePicker 隐藏一些属性

ios - 从 peoplePickerNavigationController 获取电话号码

iphone - UIAlertView 后台问题?

ios - 字符串上的 XCTAssertEqual 总是失败

ios - 使用标签和手势识别器的基本示例不起作用

ios - 确定 ABContactsHelper 的 iOS 排序顺序

ios - ABPersonViewController 的正确使用方法?

iphone - ABAddressBook 在 NSDictionary 中存储值

ios - ABPeoplePicker 选择后重新加载 UITableView

iphone - [[NSDate date] retain] 和 [[NSDate alloc] init] 的区别