【问题标题】:how to set image from UIImage in ios如何在ios中从UIImage设置图像
【发布时间】:2014-12-29 13:13:43
【问题描述】:

我正在从联系人中获取图像并将其存储为:

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

现在我再次将图像设置为:

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

但是什么也没发生。根本没有图像。我做错了什么?

更新

我也试过了:

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

像这样获取它:

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

但不工作。将 NSData 设置为图像后图像返回 nil。

【问题讨论】:

  • 尝试调试。 img 是一个有效的对象吗?是image、cell 和cell.profileImg?
  • 尝试使用类型转换UIImage *image = (UIImage *)[contactInfoDict objectForKey:@"image"];您也可以通过 img = [UIImage imageWithData:(NSData *)ABPersonCopyImageData(person)]; 获取直接图像
  • 是的,所有都是有效的对象!
  • 你可以试试我的答案并告诉我吗?
  • 嗨,我已经更新了我的答案,你可以检查一下..

标签: ios objective-c uiimage


【解决方案1】:

首先check person 有image。现在使用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");
}

【讨论】:

  • 好的,那么我如何获取图像并将其设置为视图??
  • 彻底检查我的答案。
  • 好的,我将 imgData 保存到数据库中,然后像这样使用它:NSString *str = [contactInfoDict objectForKey:@"image"]; NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding]; UIImage *image = [UIImage imageWithData:data]; cell.profileImg.image = image; 但是在将 NSData 设置为图像后,图像返回 nil。而 NSString 和 NSData 不为空。
  • 第二个选项你能帮忙吗?我刚刚更新了问题。
  • 使用stackoverflow.com/a/5308188/1106035将图像保存在数据库中,并将在NSData中获取图像,可以像这样使用 UIImage *img = [UIImage imageWithData:imgDataObtainedFromDataBase];
【解决方案2】:

您可以使用此方法获取图像:

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

}

【讨论】:

    【解决方案3】:

    您正在为键设置 UIImage。所以我认为你不需要这段代码:

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

    只是

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

    【讨论】:

      【解决方案4】:

      试试这个。

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

      之后:

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

      【讨论】:

        【解决方案5】:

        好的,所以你在这里从CGImage 创建一个UIImage 并将其存储在NSDictionary 中:

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

        但在这里你将UIImage 对象视为CGImage(请记住,你已经创建了UIImage

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

        试试这个:

        cell.profileImg.image = [contactInfoDict objectForKey:@"image"];
        

        【讨论】:

        • 它在哪一行崩溃?您可以在创建contactInfoDict 对象的位置发布代码吗?是NSMutableDictionary吗?
        • cell.profileImg.image = [contactInfoDict objectForKey:@"image"];这个。
        • 控制台说什么?
        • -[__NSCFString size]: unrecognized selector sent to instance 0x7fecf1fbf340 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString size]: unrecognized selector sent to instance 0x7fecf1fbf340'
        • contactInfoDict 是 strong 属性吗?
        【解决方案6】:

        首先将contactInfoDict 当作NSMutableDictionary 然后试试下面的代码。

        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.
        

        再次获取图像

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

        更新

        直接通过记录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];
        

        【讨论】:

        • 你服用了 NSMutableDictionary 吗?崩溃日志显示什么?你可以发布它的截图吗?它在哪一行崩溃?
        • cell.profileImg.image = ( UIImage *) [contactInfoDict objectForKey:@"image"];
        • -[__NSCFString size]: unrecognized selector sent to instance 0x7fecf1fbf340 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString size]: unrecognized selector sent to instance 0x7fecf1fbf340'
        • Prasad HotaSiba:您能帮忙介绍第二个选项吗?我刚刚更新了问题。
        • 您将 ContactDict 存储到 Sqlite?您应该在问题中提及这一点。从日志中,您似乎得到了导致其崩溃的字符串。 为什么要将图像保存到 Sqlite? 您只需保存记录 ID 并在需要时通过记录 ID 从地址簿获取图像。
        猜你喜欢
        • 2012-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-13
        相关资源
        最近更新 更多