【问题标题】:Unrecognised selector error when adding UIImage to array将 UIImage 添加到数组时出现无法识别的选择器错误
【发布时间】:2013-10-12 08:27:22
【问题描述】:

我将UIImage 存储在NSDictionary 中,然后在应用程序的另一个位置传递NSDictionary。 gridImages 是一个NSMutableArray

NSDictionary *pageDict = [pageArray objectAtIndex:0];

UIImage *pageImage = [pageDict objectForKey:@"image"];

[gridImages addObject:pageImage];

pagedict 返回

{
    class = is;
    image = "<UIImage: 0x14e20e10>";
    name = kaka;
    page = 64;
}

大概是我做的

UIImage *pageImage = [pageDict objectForKey:@"image"];

但是,当我尝试将该 UIImage 添加到数组时,我收到此错误:

-[UIImage stringByDeletingPathExtension]: unrecognized selector sent to instance 0x14e20e10

知道发生了什么吗?

编辑:

对于NSDictionary,我尝试将UIImage 存储为NSData

NSData *imageData = UIImagePNGRepresentation(textbookImage);
    [pageDict setObject:imageData forKey:@"image"];
    [pageDict setObject:textbookName forKey:@"name"];
    [pageDict setObject:page forKey:@"page"];
    [pageDict setObject:nameOfClass forKey:@"class"];

然后像这样解析它:

  NSData *imageData = [pageDict objectForKey:@"image"];
   UIImage *pageImage = [UIImage imageWithData:imageData];

但它仍然返回相同的错误

【问题讨论】:

  • 您正在调用 pageImage 上的方法 (stringByDeletingPathExtension),该方法在 UIImage 类中不存在。
  • 我在哪里调用那个方法?
  • [pageDict objectForKey:@"image"] 是图片url还是图片数据?
  • @Spenciefy 我不知道,你没有发布代码,但这就是错误所说的。
  • 我不会在任何地方处理这个问题...也许这是image = "&lt;UIImage: 0x14e20e10&gt;"; 与 "" 一起存储的结果?

标签: ios objective-c uiimage nsarray nsdictionary


【解决方案1】:

您不能将UIImage 设置为NSUserDefaults,因为UIImage非属性列表 对象。

您最好存储 文档目录中的图像路径。

这段代码会有所帮助。

//设置图片

NSString *strImagePath = //path of image from documents directory

NSMutableDictionary *dictOne = [NSMutableDictionary dictionaryWithCapacity:0];

[dictOne setObject:strImagePath forKey:@"image"];

[[NSUserDefaults standardUserDefaults] setObject:dictOne forKey:@"dict"];

// 获取图片

 NSMutableDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"dict"];

    UIImage *image = [UIImage imageWithContentsOfFile:[dict objectForKey@"image"]];

编辑:

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *strImageFileName = [documentsDirectory stringByAppendingPathComponent:@"yourImageFileName"];
 UIImage *image = // Your image;

 NSData *dataImage = UIImageJPEGRepresentation(image, 0.5f);
 /* Good image quality
        NSData *dataImage = UIImagePNGRepresentation(image); 
 */

[dataImage writeToFile:strImageFileName atomically:YES];

编码愉快。

【讨论】:

  • 如何找到 strImagePath?我的图像是直接从 iphone 相机拍摄的 uiimage
  • 当你得到图像然后将它保存到具有特定文件名的文档目录中。然后将该文件名保存到 strImagePath。明白了吗@Spencefy?
  • 谢谢,现在我将路径存储在用户默认值中。但是,当我尝试添加到数组 [gridImages addObject:pageImage]; 时,我仍然收到此错误:-[UIImage stringByDeletingPathExtension]
  • grindImages 是什么对象?
  • 问题出在我使用的 uicollectionview 中……它必须是 gridImageView.image = [gridImages objectAtIndex:indexPath.row]; 而不是 gridImageView.image = [UIImage imageNamed:[gridImages objectAtIndex:indexPath.row]];。我也使用了文档目录;它要快得多。
猜你喜欢
  • 2019-04-13
  • 1970-01-01
  • 1970-01-01
  • 2011-12-17
  • 1970-01-01
  • 2012-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多