图像未显示在UICollectionViewCell上

时间:2015-04-10 07:03:55

标签: objective-c image ios8 uicollectionview

我正在尝试在联系人上获取图片,这里我使用了UICollectionViewCell但是在集合视图中我没有获得联系人的图片,我只得到姓名和号码。这是我的代码

- (IBAction)ContactDisplay:(id)sender {

    _addressBookController = [[ABPeoplePickerNavigationController alloc] init];
    [_addressBookController setPeoplePickerDelegate:self];
    [self presentViewController:_addressBookController animated:YES completion:nil];



}
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person
{
    [self displayPerson:person];
}



    - (void)displayPerson:(ABRecordRef)person
{
    NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person,
                                                                    kABPersonFirstNameProperty);
    NSLog(@"%@",name);
    NSString* phone = nil;
    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,
                                                     kABPersonPhoneProperty);
    if (ABMultiValueGetCount(phoneNumbers) > 0) {
        phone = (__bridge_transfer NSString*)
        ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
    } else {
        phone = @"[None]";
    }
    NSLog(@"%@",phone);
    UIImage *img ;

    if (person != nil && ABPersonHasImageData(person)) {
        if ((&ABPersonCopyImageDataWithFormat) != nil ) {

            img= [UIImage imageWithData:(__bridge NSData *)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail)];
        }

    } else {

        NSString *imageUrlString = @"http://www.google.co.in/intl/en_com/images/srpr/logo1w.png";
        NSURL *url = [NSURL URLWithString:imageUrlString];
        NSData *data = [[NSData alloc] initWithContentsOfURL:url];
        img= [UIImage imageWithData:data];

    }
    NSString *string ;//
     string =[NSString stringWithFormat:@"%@",img];
    NSLog(@"%@",img);
    self.name.text=name;
    self.number.text=phone;
    [self.nameArray addObject:name];
   [self.imageArray addObject:string];
    NSLog(@"%@",self.nameArray);
      NSLog(@"%@",self.imageArray);
    [self.collectionView reloadData];


    [self.collectionView performBatchUpdates:^{
        [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
    } completion:nil];

}

最后一个像我这样的图像数组

(
    "add-button.png",
    "<UIImage: 0x17e56c80>, {148, 148}"
)

在图像阵列上,每个图像如显示.PNG格式它将显示正常,然后如何修改它。 能否请你建议我怎么解决这个问题,谢谢你。

4 个答案:

答案 0 :(得分:1)

我并不完全同意您在那里所做的一切,但我认为您的数据错误。当你取回ABPerson图像数据时,请尝试使用它。

if (person != nil) {
    CFDataRef imageData = ABPersonCopyImageData(person);
    NSData *data = CFBridgingRelease(imageData);

if (data != nil && data.length > 10){   //arbitrary length to make sure our data object isnt' really empty
    img = [UIImage imageWithData:data];

    } else {

        NSString *imageUrlString = @"http://www.google.co.in/intl/en_com/images/srpr/logo1w.png";
        NSURL *url = [NSURL URLWithString:imageUrlString];
        NSData *data = [[NSData alloc] initWithContentsOfURL:url];
        img= [UIImage imageWithData:data];

    }

然后,不要将图像作为字符串存储在阵列中。将它们存储为NSData或UIImage,但不是STRINGS。

所以

[myArray addObject:img];  //not the string.

当您稍后获取它时,请确保您将其视为图像而不是字符串

答案 1 :(得分:0)

在故事板上

,选择图像并查看属性面板。

底部有“已安装”选项。检查最顶层的“已安装”框。

installed

答案 2 :(得分:0)

我认为将图像转换为字符串

可能会出现问题
NSString *string ;//
 string =[NSString stringWithFormat:@"%@",img];

将图像添加到图像数组而不转换为字符串

[self.imageArray addObject:img];

答案 3 :(得分:0)

我在我的应用程序中这样做。假设&#39; person&#39;是ABRecordRef

NSMutableDictionary *contactInfoDict = [[NSMutableDictionary alloc]
                                 initWithObjects:@[@"", @"", @"", @""]
                                 forKeys:@[@"firstName", @"lastName", @"birthday", @"picture"]];

    CFTypeRef generalCFObject;

    // Firtname
    generalCFObject = ABRecordCopyValue(person, kABPersonFirstNameProperty);
    if (generalCFObject) {
        [contactInfoDict setObject:(__bridge NSString *)generalCFObject forKey:@"firstName"];
        CFRelease(generalCFObject);
    }

    // Lastname
    generalCFObject = ABRecordCopyValue(person, kABPersonLastNameProperty);
    if (generalCFObject) {
        [contactInfoDict setObject:(__bridge NSString *)generalCFObject forKey:@"lastName"];
        CFRelease(generalCFObject);
    }

    // Birthday
    generalCFObject = ABRecordCopyValue(person, kABPersonBirthdayProperty);
    if (generalCFObject) {
        [contactInfoDict setObject:(__bridge NSString *)generalCFObject forKey:@"birthday"];
        NSLog(@"Date : %@", [contactInfoDict objectForKey:@"birthday"]);
        CFRelease(generalCFObject);
    }

    // User image
    CFDataRef photo = ABPersonCopyImageData(person);
    if (photo) {
        CFRelease(photo);
        UIImage *image = [UIImage imageWithData:(__bridge NSData*)photo];
        [contactInfoDict setObject:image forKey:@"picture"];
    }
相关问题