将UIImage添加到数组时无法识别的选择器错误

时间:2013-10-12 08:27:23

标签: ios objective-c uiimage nsarray nsdictionary

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

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

1 个答案:

答案 0 :(得分:6)

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

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

此代码将有所帮助。

// SET Image

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

快乐的编码。