user3222991
user3222991

Reputation: 341

How to display multiselected images in iOS

I am making an app for iPhone and want to give users the ability to multiselect images from their photo-library. I already have a working code for user to select four images at a time.

But I can't select 1 or 2 or 3 images at a time. I want to select the 1 or 2 or 3 images at a time.

This is my code. I have struggled for 3 days without finding a solution. Please help me anybody.

Thanks in advance.............

-(void)choosePhotoFromExistingImages
{   
    ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initImagePicker];   
    elcPicker.maximumImagesCount = 4;   
    elcPicker.returnsOriginalImage = NO; //Only return the fullScreenImage, not the fullResolutionImage 
    elcPicker.imagePickerDelegate = self; 
    [self presentViewController:elcPicker animated:YES completion:nil];   
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{    
    [self.navigationController dismissViewControllerAnimated: YES completion: nil];
}

- (void)displayPickerForGroup:(ALAssetsGroup *)group
{   
    ELCAssetTablePicker *tablePicker = [[ELCAssetTablePicker alloc] initWithStyle:UITableViewStylePlain];   
    tablePicker.singleSelection = YES;  
    tablePicker.immediateReturn = YES;    
    ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initWithRootViewController:tablePicker];   
    elcPicker.maximumImagesCount = 0;   
    elcPicker.imagePickerDelegate = self;   
    elcPicker.returnsOriginalImage = NO; //Only return the fullScreenImage, not the fullResolutionImage 
    tablePicker.parent = elcPicker;

    // Move me   
    tablePicker.assetGroup = group;
    [tablePicker.assetGroup setAssetsFilter:[ALAssetsFilter allAssets]];
    [self presentViewController:elcPicker animated:YES completion:nil];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{   
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {   
        return YES;
    } 
    else 
    {   
        return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
    }
}

#pragma mark ELCImagePickerControllerDelegate Methods

- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{    
    [self dismissViewControllerAnimated:YES completion:nil];    
    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;        
        [self.view addSubview:imageview ];        
        self.chosenImages = images;
    }    
    UIImageView *image1=[[UIImageView alloc]initWithFrame:CGRectMake(10, 240, 40, 40)];    
    image1.image=[images objectAtIndex:0];   
    [self.view addSubview:image1];
    UIImageView *image2=[[UIImageView alloc]initWithFrame:CGRectMake(60, 240, 40, 40)];   
    image2.image=[images objectAtIndex:1];    
    [self.view addSubview:image2];
    UIImageView *image3=[[UIImageView alloc]initWithFrame:CGRectMake(120, 240, 40, 40)];    
    image3.image=[images objectAtIndex:2];    
    [self.view addSubview:image3];    
    UIImageView *image4=[[UIImageView alloc]initWithFrame:CGRectMake(180, 240, 40, 40)];   
    image4.image=[images objectAtIndex:3];   
    [self.view addSubview:image4];  
}

- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker
{   
    [self dismissViewControllerAnimated:YES completion:nil];
}

Upvotes: 0

Views: 427

Answers (2)

Anton Gaenko
Anton Gaenko

Reputation: 9025

Check your code in - (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info

For example, you create array with length of 2, but in code below you try to get image3.image=[images objectAtIndex:2]; and image4.image=[images objectAtIndex:3];. It's not a surprise that your code throws exception.

Good luck!

Upvotes: 1

moosa0709
moosa0709

Reputation: 476

Here is the link for the Tutorial. Its works fine in my project.Try this

Hope its useful for you.

Upvotes: 1

Related Questions