Re: [問題] 用NSUserDefaults 儲存 陣列圖片
※ 引述《j5307 (JJ)》之銘言:
: //有問題的地方
: [Marray addObject: thumbnailImg];
: [SaveDefaults setObject:array forKey:@"Key"];
: for (int i=0; i<Marray.count; i++) {
: NSLog(@"test:%@",[Marray objectAtIndex:i]);
: }
: 測試結果
: test:<UIImage: 0x22575960>
: 但在做第二次動作時 會把第一次蓋掉
: test:<UIImage: 0x2257a2c0>
: 更改問題的地方成 字串儲存
: [Marray addObject: @"try"];
: 測試結果
: test:try
: 第二次測試時 就會顯示兩次
: test:try
: test:try
不是第二次會把第一次蓋掉
而是因為 NSUserDefaults 根本沒把圖片存進去, 所以你第二次不會顯示第一次的值
http://goo.gl/9fkL4 [developer.apple.com]
A default object must be a property list, that is, an instance of (or for
collections a combination of instances of): NSData, NSString, NSNumber,
NSDate, NSArray, or NSDictionary.
UIImage 根本就不能存
如果你想用 NSUserDefaults 存圖片
正確的方法是使用 NSData
UIImage *image = ...; // Some image
NSData *imageData = UIImagePNGRepresentation(image); // 也有 JPEG 版
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:imageData forKey:@"image"];
[defaults synchronize];
不過要注意 NSNserDefaults 是設計給輕量資料使用的機制
存太大的東西會嚴重影響效能, 請小心使用
--
╱ ̄ ̄ ̄╲
▏◢█◣ ▏
︻█︻█ ▏ 成龍表示: 是喔...
′/ ‵ ▏
╰╯ █╱
ψQSWEET ◥︶█◤
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.32.81.146
推
11/05 14:34, , 1F
11/05 14:34, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
MacDev 近期熱門文章
PTT數位生活區 即時熱門文章