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

但是它仍然返回相同的错误

最佳答案

您不能将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];

快乐的编码。

关于ios - 将UIImage添加到数组时出现无法识别的选择器错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19332168/

10-16 15:07