Search code examples
iosobjective-cnsdocumentdirectory

array of images in document directory


I have select multiple images from iPhone through ELC controller. The images are stored in array now I want to store this array of images in document directory so please help me someone..

- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
if ([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)]){
    [self dismissViewControllerAnimated:YES completion:nil];
} else {
    [self dismissModalViewControllerAnimated:YES];
}

for (UIView *v in [_scrollView subviews]) {
    [v removeFromSuperview];
}

CGRect workingFrame = _scrollView.frame;
workingFrame.origin.x = 0;

NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];


for(NSDictionary *dict in info) {

    UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
    [images addObject:image];

    UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
    [imageview setContentMode:UIViewContentModeScaleAspectFit];
    imageview.frame = workingFrame;

    [_scrollView addSubview:imageview];
    [imageview release];

    workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
}

self.chosenImages = images;
NSLog(@"values=%@",_chosenImages);

[_scrollView setPagingEnabled:YES];
[_scrollView setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
}

Solution

    1. For each of the images in the images array, write to the file file one by one.

      NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
      
      NSInteger anIndex = 0;
      for (UIImage *anImage in images) {
         NSString *anImageName = [NSString stringWithFormat:@"%d.png", anIndex++];
         NSString *anImagePath = [NSString stringWithFormat:@"%@/%@", aDocumentsDirectory, anImageName];
      
         NSData *anImageData = UIImagePNGRepresentation(anImage);
        [anImageData writeToFile:anImagePath atomically:YES];
      }
      

    2.. When you extract the Original Image, save image to file right there.

        NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSInteger anIndex = 0;
    
        for(NSDictionary *dict in info) {
           UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
           [images addObject:image];
    
           UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
           [imageview setContentMode:UIViewContentModeScaleAspectFit];
           imageview.frame = workingFrame;
    
           [_scrollView addSubview:imageview];
           [imageview release];
    
           workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
    
           // Save Image
           NSString *anImageName = [NSString stringWithFormat:@"%d.png", anIndex++];
           NSString *anImagePath = [NSString stringWithFormat:@"%@/%@", aDocumentsDirectory, anImageName];
    
           NSData *anImageData = UIImagePNGRepresentation(image);
           [anImageData writeToFile:anImagePath atomically:YES];
    }