在nsuserdefault中存储图像数组

时间:2012-03-12 06:13:33

标签: iphone uiimageview nsuserdefaults

我创建了一个ios应用程序,我需要将图像数组存储到nsuserdefaults中。再次重新启动应用程序时,存储图像需要在滚动视图中显示。我如何存储图像数组并从nsuserdefault获取数组。预先感谢。

3 个答案:

答案 0 :(得分:1)

试试这个

NSData *dataVal = [NSKeyedArchiver archivedDataWithRootObject:val];
[defaults setObject:dataVal forKey:keyName];
[defaults synchronize];

答案 1 :(得分:1)

试试这个

for(int val=0;val<[imageArray count];val++){
// imageArray is an Array which contains the Image Names
// See below converting the 
NSData *imgData = UIImagePNGRepresentation([UIImage imageNamed:[NSString stringWithFormat:@"@%d",[imageArray objectAtIndex]val]]);
    //add this `imgData` into dataArray(Which is An NSmutableArray don't forget to Allocate it)
    [dataArray addObject:imgData];
    //then Store this Array to NSuserDefaults
[[NSUserDefaults standardUserDefaults] setObject:dataArray forKey:@"key"];
}

// the Get the Stored Data Array from NSuserDefaults.
 NSMutableArray* storedDataArray=[[NSMutableArray alloc] initWithArray:[[NSUserDefaults   standardUserDefaults]objectForKey:@"key"]];

 //`storedDataArray` array contains the ImageData(NSData)
 //Use it As you want 

答案 2 :(得分:0)

NSData *imgData = UIImageJPEGRepresentation(YOURIMAGE,1.0);
// Now store the NSData in the defaults for a  key

然后像这样加载

UIImage *image = [UIImage imageWithData:[defaults objectForKey:@"Image"]];