Search code examples
iosobjective-cimagedynamicdisplaytag

Display images dynamicalli in objective -c


I want images to be displayed dynamically, for ex- if user selects 10 images, all those should get displayed, now if user wants to select 5 more images, so (10+5) 15 images should get displayed. How to achieve this functionality?

I am using UIImagePickerController to select images, so at a time user will be able to select 1 image only.I have given a add photo button.

 UIImagePickerController *galleryObj=[[UIImagePickerController alloc]init];
    galleryObj.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    galleryObj.allowsEditing = YES;
    galleryObj.delegate=self;
    [self presentViewController:galleryObj animated:YES completion:nil];

Above will get executed when user taps on add photo button.


Solution

  • //GalleryViewcontroller.h

    #import <UIKit/UIKit.h>
    
    @interface GallaryViewController : UIViewController<UIImagePickerControllerDelegate>
    {
        NSMutableArray *selectedImagesArray;
    }
    @end
    

    GalleryViewcontroller.m

    @implementation GallaryViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    
        //allocating array
        selectedImagesArray=[[NSMutableArray alloc]init];
    }
    
    
    -(IBAction)openGallery:(id)sender
    {
        UIImagePickerController *galleryObj=[[UIImagePickerController alloc]init];
        galleryObj.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
        galleryObj.allowsEditing = YES;
        galleryObj.delegate=self;
        [self presentViewController:galleryObj animated:YES completion:nil];
    
    }
    
    #pragma mark UIImagePickerController Delegate
    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        [picker dismissViewControllerAnimated:YES completion:^{
    
    
            //here we are adding the image in array
            [selectedImagesArray addObject:[info objectForKey:UIImagePickerControllerOriginalImage]];
            NSLog(@"%@",selectedImagesArray);
    
            //reload your colleciton view or tableview or any other view which your using to show selected images
    
            //you can get images by index
            //UIImage *img=[selectedImagesArray objectAtIndex:0];
    
    
        }];
    }
    @end