G.P.Reddy
G.P.Reddy

Reputation: 556

Image not displayed on UICollectionViewCell

I am trying to get images on contacts,here i used UICollectionViewCell but in the collection view i didn't get image for the contact,i get only name and number.Here my code is

- (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];

}

finally an image array i got like this

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

On image array every image like display .PNG format it will display fine ,then how can modify it. Can you please suggest me how can you solve this,thank you.

Upvotes: 8

Views: 222

Answers (4)

David Ansermot
David Ansermot

Reputation: 6112

I do it like this in my app. Assuming 'person' is an 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"];
    }

Upvotes: 0

Gil Sand
Gil Sand

Reputation: 6036

I don't fully agree with everything you're doing there but I think you're getting your data wrong. Try using this instead when you're fetching the ABPerson image data.

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

    }

Then don't store your images as Strings in your array. Store them either as NSData or UIImage, but NOT STRINGS.

so

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

And when you fetch it later, make sure you treat is as an image and not as a string

Upvotes: 1

Ram Vadranam
Ram Vadranam

Reputation: 485

I think there might be issue with conversion of image to string

NSString *string ;//
 string =[NSString stringWithFormat:@"%@",img];

Add image to image array without converting to string

[self.imageArray addObject:img];

Upvotes: 0

tt_emrah
tt_emrah

Reputation: 1053

on your storyboard, select the image and look at the properties panel.

there are "Installed" options at the bottom. check the topmost "Installed" box.

installed

Upvotes: 0

Related Questions