简体   繁体   English

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

[英]how to save camera capture images in nsuserdefaults in ios

I would like to save the camera capture images in nsuserdefaults. 我想将相机拍摄的图像保存在nsuserdefaults中。 To add the camera capture images in collection view. 若要在集合视图中添加相机捕获的图像。 (I added it in Cemeracaptureviewcontroler) my problem is: create one array and add the capture images collection view is working fine. (我在Cemeracaptureviewcontroler中添加了它)我的问题是:创建一个数组并添加捕获图像集合视图工作正常。 but when I moving back to another view controller and come back again to Cemeracaptureviewcontroler then capture images are not display. 但是当我移回另一个视图控制器并再次回到Cemeracaptureviewcontroler时,捕获的图像不会显示。 In array values the images are display null . 在数组值中,图像显示为null when the images save means that image array like this values. 图片保存时表示该图片数组的值与此类似。

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

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

So I think store the images in user defaults and display in collection. 因此,我认为将图像存储在用户默认设置中并在集合中显示。 How can I solve this issues help me. 我如何解决此问题对我有帮助。

///here i get the capture image in picker ///在这里,我在选择器中获取捕获图像

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];
}

You can save the images into NSUserDefults as, 您可以将图像另存为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];
}

and retrieve as, 并检索为

- (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];
}

Hope this may help you 希望这对您有帮助

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

UIImage *browsed_Image = [UIImage imageWithData:UIImageJPEGRepresentation([info objectForKey:UIImagePickerControllerEditedImage], 0.2)];

[picker dismissViewControllerAnimated:YES completion:^{
    if (browsed_Image) {
            dispatch_queue_t queue = dispatch_get_main_queue();
            dispatch_async(queue, ^{
                [[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(browsed_Image) forKey:@"keyname"];
            });
    }
}];
}

do not save images in defaults use.. 不要保存默认使用的图像。

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

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    UIImage  *image= [info objectForKey:UIImagePickerControllerEditedImage];
    NSData *data = UIImagePNGRepresentation(image);
    NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
    NSNumber *timeStampObj = [NSNumber numberWithDouble:timeStamp];
    NSString *fileName = nil;
    fileName = [NSString stringWithFormat:@"PICT_%@.png", [timeStampObj stringValue]];
    NSString *localFilePath = [dataPath stringByAppendingPathComponent:fileName];
    [data writeToFile:localFilePath atomically:YES];
    dispatch_async(dispatch_get_main_queue(), ^{
      //if any UI task
          });
    });
}

datPath is dataPath = [documentsDirectory stringByAppendingPathComponent:@"<your relevent subdirectoryname>"]; datPath是dataPath = [documentsDirectory stringByAppendingPathComponent:@"<your relevent subdirectoryname>"]; now save the file path "localFilePath" in array and use it you can maintain its persistance by keeping its retain count higher or simply save this array of file paths in NSUserDefaults 现在将文件路径“ localFilePath”保存在数组中,并使用它,可以通过保持较高的保留计数来保持其持久性,或者只是将此文件路径数组保存在NSUserDefaults

you can set image as 您可以将图像设置为

[UIImage imageWithContentsOfFile:<localFilePath>]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM