繁体   English   中英

从地址簿中获取图像并将其存储到 NSmutablearray 但在显示它时会在 iphone sdk 中出现“EXCBAD 错误”?

[英]get image from address book and stored it to NSmutablearray but while showing it gives `EXCBAD Error` in iphone sdk?

嗨,我正在创建自定义地址簿,我只想让拥有 email 地址的用户一切正常,但是当我尝试在 tableview 中显示图像时,它给我EXCBAD Error 我有这样的代码

            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 whats wrong in that code? 如何将图像存储在 NSMutabeArray 中并显示图像?

嘿,帮助我,任何人都可以帮助我或建议我像Google Latitude一样实施,您可以在 iPhone 地址簿中添加朋友中看到我只想实施一些类似于此应用程序的功能,我的所有代码都运行良好,只是我做不到从地址簿中获取图像请帮助我....

我已经像这样将图像存储在 NSMutableDictionary 的 object 中

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"];
            }

您可以操纵代码以满足您的要求,我使用的是字典,而您使用的是 arrays,因此您可以使用数组的 addObject 方法来存储图像,

我总是喜欢从具有多个字典的可变数组中的地址簿中获取记录,这样操作记录就变得容易了。

尝试检查图像是否可用或不使用

if( ABPersonHasImageData( person ) ) { UIImage* img = [UIImage imageWithData:(NSData *)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail)] }

您需要检查是否有图像可用的条件。

NSData *data = (NSData *)ABPersonCopyImageData(person);
UIImage *img = [UIImage imageWithData:data];

if(img != nil)
{
  // assign the image here
}
else
{
  img = [UIImage imageNamed:@"icon.png"];
}

我刚刚清除了你的代码。 我认为这可能会有所帮助。

您不能将图像 object 直接添加到数组中,您需要先将图像转换为 NSData 并添加到数组中,然后在显示图像时将 NSdata 转换为图像。 希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM