when i capture the image i need to save images one by one in table view like below image .is need use nsuser defaults or use core data? and after picking the image how to add to Array
Asked
Active
Viewed 2,924 times
-1
-
Just save image in directory with unique name, Store that unique name into array and Retrieve image from directory, show in your tableview. If you want Answer then I will post it – Jitendra Modi Oct 21 '16 at 06:27
-
If you wish to save images in default photo library then use.. https://stackoverflow.com/a/44328085/5315917 – Dimple Shah Jun 02 '17 at 11:44
3 Answers
1
You can save image in NSUserDefaults as follows,
-(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];
}

Tejas Patel
- 850
- 10
- 26
0
Yes, you can save the images in the document directory and keep the image file names in an array after that retrieve that images from doc. dir. just like this
// 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**
Then retrieve from doc. dir. from iterate the array.
//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];
Thank you...happy coding.

Sailendra
- 1,318
- 14
- 29
-
could u explain please little confusion how add imageas one by one to array how display in tableview – SWAMY CHUNCHU Oct 25 '16 at 06:14
-
-
After taking image just save in to doc. directory and save the file name in a db. Then retrieve all the file names from db and used for table set. In cellForRowAtIndexPath get the file name and retrieve from doc. dir., set into the image view – Sailendra Oct 25 '16 at 08:31
0
Try below this code: suppose you want to image name also display use this code,
- (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;}

G.Anushiya
- 31
- 5