2

我将 a 存储UIImage在 aNSDictionary中,然后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,我尝试将其存储UIImageNSData

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

但它仍然返回相同的错误

4

1 回答 1

6

您不能按原样设置UIImage为非属性列表对象。NSUserDefaultsUIImage

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

这段代码会有所帮助。

// 设置图像

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

快乐编码。

于 2013-10-12T09:40:03.197 回答