我将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];
但它仍然会返回相同的错误
答案 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];
快乐的编码。