【问题标题】:after picking the image how save images locally in ios选择图像后如何在 ios 中本地保存图像
【发布时间】:2017-03-03 08:28:04
【问题描述】:

当我捕获图像时,我需要在表格视图中一张一张地保存图像,如下图所示。是需要使用 nsuser 默认值还是使用核心数据? 以及选择图像后如何添加到数组

【问题讨论】:

  • 只需将图像保存在具有唯一名称的目录中,将该唯一名称存储到数组中并从目录中检索图像,显示在您的表格视图中。如果你想要答案,那么我会发布它
  • 如果您希望将图像保存在默认照片库中,请使用..stackoverflow.com/a/44328085/5315917

标签: ios objective-c ios7 ios5 ios7.1


【解决方案1】:

您可以将图像保存在 NSUserDefaults 中,如下所示,

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

【讨论】:

    【解决方案2】:

    是的,您可以将图像保存在文档目录中,然后将图像文件名保存在数组中,然后从 doc 中检索图像。目录就这样

    // For error information
        NSError *error;
        NSFileManager *fileMgr = [NSFileManager defaultManager];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/YOUR_IMG_FOLDER"];
    
        if (![fileMgr fileExistsAtPath:dataPath])
            [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
    
        //Get the current date and time and set as image name
        NSDate *now = [NSDate date];
    
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"yyyy-MM-dd_HH-mm-ss";
        [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
        NSString *gmtTime = [dateFormatter stringFromDate:now];
        NSLog(@"The Current Time is :%@", gmtTime);
    
        NSData *imageData = UIImageJPEGRepresentation(_postImage, 0.5); // _postImage is your image file and you can use JPEG representation or PNG as your wish
        int imgSize = imageData.length;
        ////NSLog(@"SIZE OF IMAGE: %.2f Kb", (float)imgSize/1024);
    
        NSString *imgfileName = [NSString stringWithFormat:@"%@%@", gmtTime, @".jpg"];
        // File we want to create in the documents directory
         NSString *imgfilePath= [dataPath stringByAppendingPathComponent:imgfileName];
        // Write the file
        [imageData writeToFile:imgfilePath atomically:YES];
    
         **// Keep your Image file name into an mutable array here OR you can saved the array in a UserDefaults**
    

    然后从 doc 中检索。目录from 迭代数组。

    //Get image file from sand box using file name and file path
    NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"YOUR_IMG_FOLDER"];
    stringPath  = [stringPath stringByAppendingPathComponent:imgFile]; // imgFile to get from your array, where you saved those image file names  
    UIImage *image = [UIImage imageWithContentsOfFile:stringPath];
    

    谢谢...祝您编码愉快。

    【讨论】:

    • 你能解释一下如何将图像一张一张添加到数组中如何在tableview中显示
    • 从相机拍摄图像
    • 拍照后保存到文档中。目录并将文件名保存在数据库中。然后从 db 中检索所有文件名并用于表集。在 cellForRowAtIndexPath 中获取文件名并从 doc 中检索。 dir.,设置到图像视图中
    【解决方案3】:

    试试下面的代码: 假设你想图像名称也显示使用此代码,

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
      chosenImage = info[UIImagePickerControllerOriginalImage];
      chosenImage = [UIImage unrotateImage:chosenImage];    
      addGalleryImageview.image = chosenImage;
      isCameraOn = YES;    
      NSString* fileName = [NSString stringWithFormat:@"gallery%@",[Utils getDateString]];
      imageNamelbl.text = fileName;  
      [picker dismissViewControllerAnimated:YES completion:NULL];}
    
    
    - (UIImage*)unrotateImage:(UIImage*)image{
       CGSize size = image.size;
       UIGraphicsBeginImageContext(size);
       [image drawInRect:CGRectMake(0,0,size.width ,size.height)];
       UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
       UIGraphicsEndImageContext();   
       return newImage;}
    

    【讨论】:

      猜你喜欢
      • 2012-07-24
      • 1970-01-01
      • 2015-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      相关资源
      最近更新 更多