gpt4 book ai didi

ios - 我可以使用 NSUserDefaults 保存应用程序的表格单元格值吗?

转载 作者:行者123 更新时间:2023-11-29 03:00:28 24 4
gpt4 key购买 nike

我有一个由标题和图像条目填充的表格。这是通过以下方法完成的:

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 是正确的还是有更好的实践来实现这个目标?

最佳答案

NSUserDefaults 在访问时完全加载到内存中。这意味着,如果您的数据源包含多个项目,那么速度会非常慢,甚至在手机内存不足时可能会崩溃。

但是,当我理解您的代码正确时,您会将照片存储在相机胶卷中。因此,您已经拥有某种图像 URL(代码中的 assetURL)。

对于一种快速而肮脏的解决方案(我发现对于不经常更改的少量数据来说还可以,我使用 NSCoding。因此您可以添加标题和 assetURL NSDictionary 中,并将字典添加到数据源的数组中。然后在 dealloc 中调用

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

viewDidLoad 中,您可以通过调用获取数据:

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

如果数据包含很多项目或者项目即将发生很大变化,我倾向于使用 Core Data 或 Sqlite。如何使用它们不适合这个答案。

关于ios - 我可以使用 NSUserDefaults 保存应用程序的表格单元格值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23370552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com