Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 722
G.P.Reddy Avatar asked Apr 10 '15 07:04

G.P.Reddy


1 Answers

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

like image 169
Gil Sand Avatar answered Nov 06 '22 07:11

Gil Sand