-2

I am fetching an image from contact and storing it like:

NSData *contactImageData = (__bridge NSData *)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail);
UIImage  *img = [UIImage imageWithData:contactImageData];
NSLog(@"%@",img); //returns like '<UIImage: 0x7fc740460260>'

[contactInfoDict setObject:img forKey:@"image"]; //contactInfoDict is a NSDictionary

Now again I'm setting the image like:

UIImage *image = [UIImage imageWithCGImage:(__bridge CGImageRef)([contactInfoDict objectForKey:@"image"])];
cell.profileImg.image = image;

But nothing happening. there is no image at all. What I did wrong?

Update

I also tried:

NSData *contactImageData = (__bridge NSData *)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail);
[contactInfoDict setObject:contactImageData forKey:@"image"];

fetching it like:

 NSString *str = [contactInfoDict objectForKey:@"image"];
 NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding];
 UIImage *image = [UIImage imageWithData:data];
 cell.profileImg.image = image;

But not working. After setting NSData to image image returns nil.

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
Poles
  • 3,585
  • 9
  • 43
  • 91

6 Answers6

1

Firstly check person has image. Now use ABPersonCopyImageDataWithFormat

if (ABPersonHasImageData(yourPersonObject)) 
{
   //person has image
   //ARC need to take ownership of the Core Foundation object . Use CFBridgingRelease
   //Your mistake is here where i have changed
   NSData *imgData = CFBridgingRelease(ABPersonCopyImageDataWithFormat(yourPersonObject, kABPersonImageFormatThumbnail));

   if(imgData)
   {
      UIImage  *img = [UIImage imageWithData:imgData];
      if(img)
      {
         [contactInfoDict setObject:img forKey:@"image"];
      }
   }
}
else
{
   NSLog(@"no image");
}
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • Ok then How do I fetch the image and set it to a view?? – Poles Dec 29 '14 at 13:24
  • Check my answer completely. – Paresh Navadiya Dec 29 '14 at 13:26
  • Ok, I'm saving the **imgData** into database and later using it like: `NSString *str = [contactInfoDict objectForKey:@"image"]; NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding]; UIImage *image = [UIImage imageWithData:data]; cell.profileImg.image = image;` But after setting NSData to image image returns nil. whereas NSString and NSData is not null. – Poles Dec 29 '14 at 13:51
  • Can you help with the second option? I just updated the question. – Poles Dec 29 '14 at 14:14
  • Use http://stackoverflow.com/a/5308188/1106035 to save image in database and will get image in NSData which can be used like this UIImage *img = [UIImage imageWithData:imgDataObtainedFromDataBase]; – Paresh Navadiya Dec 29 '14 at 14:19
1

You Can use this method to get Image :

 - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
                             didSelectPerson:(ABRecordRef)person
    {
    if (ABPersonHasImageData(person)) {
        NSData *contactImageData = (__bridge NSData *)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail);

      UIImage *image=[UIImage imageWithData:contactImageData];
    }

    [_addressBookController dismissViewControllerAnimated:YES completion:nil];

}
Dheeraj Singh
  • 5,143
  • 25
  • 33
0

try this.

NSData *imagedata = (__bridge NSData *)(ABPersonCopyImageData(person));
UIImage  *img;
if(imagedata !=nil)
img = [UIImage imageWithData:contactImageData];
else  // assign it a temporary image.
img = [UIImage imageNamed:@"TempImage"];

[contactInfoDict setObject:img forKey:@"image"];

After this :

UIImage *image = [UIImage imageWithCGImage:(__bridge CGImageRef)([contactInfoDict objectForKey:@"image"])];
    cell.profileImg.image = image;
Dheeraj Singh
  • 5,143
  • 25
  • 33
0

You are setting UIImage for key. So I think you do not need this code:

UIImage *image = [UIImage imageWithCGImage:(__bridge CGImageRef)([contactInfoDict objectForKey:@"image"])];
cell.profileImg.image = image;

Just

UIImage *image = (UIImage*)[contactInfoDict objectForKey:@"image"];
cell.profileImg.image = image;
Sergey Pekar
  • 8,555
  • 7
  • 47
  • 54
0

First of all Take contactInfoDict as a NSMutableDictionary then Try the following code.

NSData *contactImageData = (__bridge NSData *)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail);
[contactInfoDict setObject:[UIImage imageWithData:contactImageData]; forKey:@"image"]; 

// As its an Mutable dictionary, it will save the Value for sure.

To get the Image again

cell.profileImg.image = ( UIImage *) [contactInfoDict objectForKey:@"image"];

Update

To get the image from Address Book directly by its record id

ABAddressBookRef addressBook = ABAddressBookCreate( ); 
NSNumber *recordId = [NSNumber numberWithInteger:[record_str integerValue]];
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook,recordId.integerValue);
NSData *contactImageData = (__bridge NSData *)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail);
cell.profileImg.image = [UIImage imageWithData:contactImageData];
Siba Prasad Hota
  • 4,779
  • 1
  • 20
  • 40
  • Did you take **NSMutableDictionary** ? What the Crashlog Shows? Can you post a screenshot of it? in Which line its Crashing? – Siba Prasad Hota Dec 29 '14 at 13:46
  • `cell.profileImg.image = ( UIImage *) [contactInfoDict objectForKey:@"image"];` – Poles Dec 29 '14 at 14:01
  • `-[__NSCFString size]: unrecognized selector sent to instance 0x7fecf1fbf340 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString size]: unrecognized selector sent to instance 0x7fecf1fbf340'` – Poles Dec 29 '14 at 14:02
  • Prasad HotaSiba: Can you help with the second option? I just updated the question. – Poles Dec 29 '14 at 14:13
  • You are storing the ContactDict to Sqlite? You should Mention this in the Question. From the log it seems you are getting the String that is the reason its crashing. **Why you saving image to Sqlite?** you just save the Record id & whenever required get the Image from addressbook by the record id. – Siba Prasad Hota Dec 29 '14 at 14:24
  • can you explain it a little bit. I mean how to do it? – Poles Dec 29 '14 at 14:26
  • First you need to get the RecordID for that Contact. Check here : http://stackoverflow.com/questions/14055438/how-to-get-abrecordref-from-abrecordid When you need to display the Image, use the same record id and get the Image from Addressbook. – Siba Prasad Hota Dec 29 '14 at 14:28
0

Okay so here you create a UIImage from CGImage and store it in a NSDictionary:

NSData *contactImageData = (__bridge NSData *)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail);
UIImage  *img = [UIImage imageWithData:contactImageData];
NSLog(@"%@",img); //returns like '<UIImage: 0x7fc740460260>'
[contactInfoDict setObject:img forKey:@"image"];

But here you treat a UIImage object as a CGImage (remember, you already created a UIImage

UIImage *image = [UIImage imageWithCGImage:(__bridge CGImageRef)([contactInfoDict objectForKey:@"image"])];
cell.profileImg.image = image;

Try this instead:

cell.profileImg.image = [contactInfoDict objectForKey:@"image"];
dariaa
  • 6,285
  • 4
  • 42
  • 58
  • Which line does it crash at? can you post code where you create a `contactInfoDict` object? Is it a `NSMutableDictionary`? – dariaa Dec 29 '14 at 13:51
  • `cell.profileImg.image = [contactInfoDict objectForKey:@"image"];` this one. – Poles Dec 29 '14 at 13:52
  • What does the console say? – dariaa Dec 29 '14 at 13:53
  • `-[__NSCFString size]: unrecognized selector sent to instance 0x7fecf1fbf340 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString size]: unrecognized selector sent to instance 0x7fecf1fbf340'` – Poles Dec 29 '14 at 13:56
  • Is `contactInfoDict` a `strong` property? – dariaa Dec 29 '14 at 14:00
  • This looks like the reason. Make sure your `contactInfoDict` is not released before you try to get an image out of it. – dariaa Dec 29 '14 at 14:04
  • but I'm using contactInfoDict to store into SQLlite. and later fetching the data from database and store it into contactInfoDict again and retrieving the image. – Poles Dec 29 '14 at 14:10
  • Can you help with the second option? I just updated the question. – Poles Dec 29 '14 at 14:13
  • You are storing the ContactDict to Sqlite? You should Mention this in the Question because, from the log it seems you are getting the String that is the reason its crashing while assigning to imageview. **Why you saving image to Sqlite?** you just save the Record id & whenever required get the Image from addressbook by the recordid. – Siba Prasad Hota Dec 29 '14 at 14:21
  • You are putting a `UIImage` object into `contactInfoDict` for "image" key, so why do you expect that to get a `NSString` instead of `UIImage`? Your "fetching" should look like `cell.profileImg.image = [contactInfoDict objectForKey:@"image"];` – dariaa Dec 29 '14 at 14:21