0

I have used the Imagepickerview controller to pick the photo and display it in uiview using imageview but my problem is using imageview only one image can able to display not two. its keep on replacing the existing one if i select second one. please give some suggestions how to display two photos.

here is my source code.


-(void) ViewDidLoad
{
      attachPhotoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    attachPhotoBtn.frame = CGRectMake(400, 125, 44, 44);
    UIImage *attachImg = [UIImage imageNamed:@"album_add_off.png"];
    [attachPhotoBtn setImage:attachImg forState:UIControlStateNormal];
    [attachPhotoBtn addTarget:self action:@selector(attachPhoto:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:attachPhotoBtn];

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 125, 64, 52)];
//        imageView.backgroundColor = [UIColor greenColor];
        [self.view addSubview:imageView];
    }

}


- (IBAction)attachPhoto:(id)sender {

    [sender setImage:[UIImage imageNamed:@"album_add.png"] forState:UIControlStateNormal];

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    {
        UIImagePickerController *imagePicker =[[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage,(NSString *)kUTTypeVideo,nil];
        imagePicker.allowsEditing = NO;

        // On iPad use pop-overs.
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {
            _popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
            [_popover presentPopoverFromRect:attachPhotoBtn.frame
                                          inView:self.view
                        permittedArrowDirections:UIPopoverArrowDirectionUp
                                        animated:YES];
            }
        }
        else
        {
            // On iPhone use full screen presentation.

            // [[self presentingViewController] presentViewController:imagePicker animated:YES completion:nil];
        }


        newMedia = NO;
    }


#pragma mark Image picker controller delegate methods

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

//    [self dismissModalViewControllerAnimated:YES];


    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {

        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        imageView.image = image;



    [picker dismissViewControllerAnimated:NO completion:nil];
}
temp
  • 67
  • 6

2 Answers2

2

You are using a single UIImageView component to show images, everytime you will pick an image it will be overridden by the latest one..

There are multiple ways of displaying multiple images.

  1. You can create UIImageView component dynamically when you get image in (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info function..

  2. You can use a NSMutableArray to put all your images and can show in UITableView.

  3. Or, you can add imageviews in ScrollView.

But ultimately you will need multiple UIImageView components to show multiple images not one.

Hope this helps.

iphonic
  • 12,615
  • 7
  • 60
  • 107
0

You can get all images from photo library using assest library,

1. Add AssetsLibrary.framework

2. #import "AssetsLibrary/AssetsLibrary.h"

3. get all result in array

here is code

void (^myAssetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
        if(result != NULL)
        {
            NSLog(@"See Asset: %@", result);

            if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto])
            {
                [assetsArray addObject:result];
            }
        }
    };

    //This block of code used to enumerate ALAssetsGroup.
    void (^myAssetGroupEnumerator)( ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
        if(group != nil) {
            [group enumerateAssetsUsingBlock:myAssetEnumerator];

            [self createScrollView];//Or you can use your `assetsArray` data, in UITableView,UICollectionView 

        }
        //[activity stopAnimating];
        // [activity setHidden:YES];
    };

    library = [[ALAssetsLibrary alloc] init];

    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
                           usingBlock:myAssetGroupEnumerator
                         failureBlock: ^(NSError *error) {
                             NSLog(@"Failure");
                         }
     ];

4. Get Thumbnail image

ALAsset *asset=[assetsArray objectAtIndex:i]; 
UIImage*image = [UIImage imageWithCGImage:[asset thumbnail]];
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74