gpt4 book ai didi

ios - 如何在 ios 的 nsuserdefaults 中保存相机捕获的图像

转载 作者:行者123 更新时间:2023-11-28 19:55:02 24 4
gpt4 key购买 nike

我想将相机捕获的图像保存在 nsuserdefaults 中。在 Collection View 中添加相机捕获图像。 (我在 Cemeracaptureviewcontroler 中添加了它)我的问题是:创建一个数组并添加捕获图像 Collection View 工作正常。但是当我回到另一个 View Controller 并再次回到 Cemeracaptureviewcontroler 时,捕获的图像不会显示。在数组值中,图像显示为 null。当图像保存时意味着图像数组像这样的值。

<UIImage: 0x7ae93f50> size {640, 424} orientation 0 scale 1.000000">

<UIImage: 0x7ae93f52> size {640, 424} orientation 0 scale 1.000000">

所以我认为将图像存储在用户默认值中并显示在集合中。我怎样才能解决这个问题帮助我。

///这里我在选择器中获取捕获图像

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:nil];
}

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

[picker dismissViewControllerAnimated:YES completion:Nil];

originalImage = [info objectForKey:UIImagePickerControllerEditedImage];
recipeImageView.image = [originalImage fixOrientation];

[self.phto_arr_ addObject:originalImage];
[_collectionView reloadData];
}

最佳答案

您可以将图像保存到 NSUserDefults 中,

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
if(!self.phto_arr_){
self.phto_arr_ = [[NSMutableArray alloc] init];
}
[self.phto_arr_ addObject: chosenImage];
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:self.phto_arr_];
[[NSUserDefaults standardUserDefaults] setObject: encodedObject forKey:@"images"];
[[NSUserDefaults standardUserDefaults] synchronize];
[picker dismissViewControllerAnimated:YES completion:NULL];
}

并检索为,

- (void)viewWillAppear:(BOOL)animated {

NSUserDefaults *userDefaults = (NSUserDefaults *)[NSUserDefaults standardUserDefaults];
NSData *encodedObject = [userDefaults objectForKey:@"images"];
self.phto_arr_ = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
[_collectionView reloadData];
[super viewWillAppear:animated];
}

希望对你有帮助

关于ios - 如何在 ios 的 nsuserdefaults 中保存相机捕获的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26987510/

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