2014-04-29 47 views
0

我有一個由標題和圖像條目填充的表格。這可以通過以下方法進行:我可以使用NSUserDefaults來保存應用程序的表格單元格值嗎?

tablePhotoViewController.m

- (IBAction)takePicture:(UIBarButtonItem *)sender { 

// check #1 - make sure our source type is supported 
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 

    NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]; 

    // check #2 see if media type includes images 
    if ([mediaTypes containsObject:(NSString *)kUTTypeImage]) { 

     // create our image picker 
     UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
     picker.delegate = self; 
     picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
     picker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage]; 
     picker.allowsEditing = YES; 
     [self presentViewController:picker animated:YES completion:nil]; 
    } 
} 
} 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
// do something with this image 
UIImage *imagefromcamerabutton = [info objectForKey:UIImagePickerControllerEditedImage]; 

// handle the case where editting was not allowed... 
if (!imagefromcamerabutton) imagefromcamerabutton = [info objectForKey:UIImagePickerControllerOriginalImage]; 

// save to photo albumn 
ALAssetsLibrary *al = [Utils defaultAssetsLibrary]; 
[al writeImageToSavedPhotosAlbum:[imagefromcamerabutton CGImage] metadata:nil 
       completionBlock:^(NSURL *assetURL, NSError *error) 
{ 
    // once we know it's saved, grab the ALAsset and store 
    // it in our collection for display later 
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) 
    { 
     [self.photos addObject:myasset]; 
     [self.tableView reloadData]; 
    }; 

    ALAssetsLibrary *assetslibrary = [Utils defaultAssetsLibrary]; 
    [assetslibrary assetForURL:assetURL 
        resultBlock:resultblock 
        failureBlock:nil]; 
}]; 
[self dismissImagePicker]; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return [self.photos count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

// Configure the cell... 
ALAsset *asset = [self.photos objectAtIndex:indexPath.row]; 

[cell.imageView setImage:[UIImage imageWithCGImage:[asset thumbnail]]]; 
[cell.textLabel setText:[NSString stringWithFormat:@"Thing %d", indexPath.row+1]]; 

return cell; 
} 

這個效果很好,但問題是,用戶關閉應用程序後,該單元的數據被擦除。我希望這些能夠保持照片不斷與表格相關聯,除非用戶點擊按鈕將其從陣列中刪除。從我對這項工作的理解有限,看起來我需要以某種方式實施NSUserDefaults是對的還是有更好的做法來實現這一目標?

+0

NSUserDefaults只能用於保存少量數據,比如幾個字符串或數字。使用文件存儲實際數據。 – rmaddy

+0

@rmaddy你有沒有參考資料,我可以在這裏瞭解更多關於使用文件存儲數據的信息? – Presto

回答

1

NSUserDefaults被訪問時被完全加載到內存中。這意味着如果您的數據源包含多個項目,這將非常慢,甚至在手機內存不足時甚至會崩潰。

但是,當我明白你編碼正確,你正在照相機卷存儲照片。因此,您已經有一些圖像的URL(代碼中的assetURL)。

對於一個快速和一種骯髒的解決方案(我覺得這對於少量數據的不經常改變OK,我用NSCoding。所以,你可以添加標題和assetURLNSDictionary,並添加字典到。數組數據源。然後在dealloc通話

[NSKeyedArchiver archiveRootObject:self.myDataSourceArray toFile:[self pathToFileInDocumentsDirectory]]; 

viewDidLoad那麼你就可以得到數據支持你撥打:

self.myDataSourceArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[self pathToFileInDocumentsDirectory]]; 

如果數據包含很多項目或者項目將要改變很多,我傾向於使用Core Data或Sqlite。如何使用這些不適合這個答案。

+0

謝謝,這是一個很好的解決方案,這個答案。我將考慮核心數據,因爲我需要將更多數據與圖像關聯起來。 – Presto

相關問題