1

I am new in iPhone App Development. In my code, i am able to select multiple images from the library and get the path of only 1 image. Then i am creating a copy of that image in Documents directory in a folder called "images" and trying to zip it. Now i want to get the path of all those selected multiple images, copy them in documents directory in the same folder "images" and i want to zip them later. Please tell me how i can do the above mentioned tasks in my code. This is how my code looks as of now:

- (void) imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   if (imagePickerController.allowsMultipleSelection) {
    NSArray *mediaInfoArray = (NSArray *)info;
    NSLog(@"Selected %d photos", mediaInfoArray.count);
    NSData *webData = UIImagePNGRepresentation([[mediaInfoArray objectAtIndex:0] objectForKey:@"UIImagePickerControllerOriginalImage"]);
   NSLog(@"web data length is: %u",[webData length]);
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *localFilePath = [documentsDirectory stringByAppendingString:@"/images.png"];
   [webData writeToFile:localFilePath atomically:YES];

   NSLog(@"localFilePath.%@",localFilePath);
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
vanya
  • 59
  • 3
  • 11

1 Answers1

1

I used the same QBImagePickerController and have added multiple images in an NSMutableArray using this method below. Please check, also once they are added to the array you can add them wherever you require.

- (void)imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingMediaWithInfo:(id)info
    {
        if(imagePickerController.allowsMultipleSelection)
        {
            NSArray *mediaInfoArray = (NSArray *)info;        
            for (int i =0; i<mediaInfoArray.count ; i++)
            {
                NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"/Folder/Selected Files"];
                dispatch_async(dispatch_get_main_queue(), ^{
                    NSData *data = UIImageJPEGRepresentation([[mediaInfoArray valueForKey:@"UIImagePickerControllerOriginalImage"] objectAtIndex:i], 1.0);
                    NSString *fileName = [stringPath stringByAppendingPathComponent:[NSString stringWithFormat:@"Image%d.png",i]];
                    [data writeToFile:fileName atomically:YES];
                });
                [ImagesArray addObject:[[mediaInfoArray valueForKey:@"UIImagePickerControllerOriginalImage"] objectAtIndex:i]];
            }
        }
        else
        {
            //NSDictionary *mediaInfo = (NSDictionary *)info;
            //NSLog(@"Selected: %@", mediaInfo);
        }
        }       
        [TableView reloadData];        
        [self dismissViewControllerAnimated:YES completion:NULL];
    }

Hope this helps ...

EDIT :

Well I gave you a better solution as saving in the Cache directory is always better than in the documents directory please check is saving in NSDocumentDirectory okay?

But if you want to save it in the Documents directory anyway then you can simply replace

NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"/Folder/Selected Files"];

with

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
Community
  • 1
  • 1
IronManGill
  • 7,222
  • 2
  • 31
  • 52
  • Ok Thanks @IronManGill. I will try in my code and will check once. – vanya Aug 20 '13 at 05:34
  • Iam not getting. I want to create a copy of those selected images in documents directory in folder. When i copied your code, the folder is getting created in Library->cache. – vanya Aug 20 '13 at 06:23
  • Now i am able to see the path of created folder and the number of selected images's details in console. But when i check in finder, i am not able to see the folder inside documents directory. – vanya Aug 20 '13 at 06:42
  • Hey thanks a lot. Its working now after editing the code according to my requirements. – vanya Aug 20 '13 at 11:08
  • Ya the output is correct. But i have one last question regarding dynamic folder creation. – vanya Aug 20 '13 at 11:15
  • What is the question ? – IronManGill Aug 20 '13 at 11:17
  • Till now i was hard coding the folder name to store the zipped images. But now i want to make it dynamic by typing in textfield. Please tell me how to do this? – vanya Aug 20 '13 at 11:21
  • I searched in Google a lot, but dint find any relevant solution for this. – vanya Aug 20 '13 at 11:34