Search code examples
iosobjective-cuiimageviewios-camera

Display photos in different UIImageViews


In my test app, I have two buttons (takePhoto1 and takePhoto2) and two UIImageViews (photo1View and photo2View). The first button takes a photo and displays it in the first image view. The second button takes another photo, and is supposed to display it in the second image view. However, for some reason second photo is displayed in photoView1 instead of photoView2.

I haven't implemented any code to save the photos yet, so I'm not worried about the photos persisting in the image views.

What I do need to figure out is how to make sure when the second photo is taken, it is displayed in the appropriate view. Here is what I have so far:

//Take photo1 and display in top image view
- (void)takePhoto1;
{
    [self dismissViewControllerAnimated:YES completion:NULL];

    if ([self.capturedImages count] > 0)
    {
        if ([self.capturedImages count] == 1)
        {
            // Camera took a single picture.
            [self.photo1View setImage:[self.capturedImages objectAtIndex:0]];
        }
        // To be ready to start again, clear the captured images array.
        [self.capturedImages removeAllObjects];
    }

    self.imagePickerController = nil;
}




//Take photo2 and display in bottom image view
- (void)takePhoto2;
{
    [self dismissViewControllerAnimated:YES completion:NULL];

    if ([self.capturedImages count] > 0)
    {
        if ([self.capturedImages count] == 1)
        {
            // Camera took a single picture.
            [self.photo2View setImage:[self.capturedImages objectAtIndex:1]];
        }
        // To be ready to start again, clear the captured images array.
        [self.capturedImages removeAllObjects];
    }

    self.imagePickerController = nil;
}

Anyone know how to fix this?


Solution

  • I saw your questions an couldn't help my self to solve it in my way. If I had same problem as yours I would have solved it in this way:

    Consider, button's tag is same as, imageView tag.

    #import "ViewController.h"
    
    @interface ViewController ()
    - (IBAction)takePhoto:(id)sender;
    
    @property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *imageViews;
    
    @property int tag;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (IBAction)takePhoto:(id)sender {
        _tag = ((UIButton *)sender).tag;
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        [self presentViewController:imagePickerController animated:YES completion:nil];
    
    
    }
    
    #pragma mark - UIImagePickerDelegate Methods
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
        [picker dismissViewControllerAnimated:NO completion:nil];
    
        [[self getImageView:_tag] setImage:image];
    
    }
    
    - (UIImageView *) getImageView : (int) tag{
        for (UIImageView *imageView in _imageViews) {
            if (imageView.tag == tag) {
                return imageView;
            }
        }
        return nil;
    }
    
    
    @end
    

    Here you can check this as working project here.