У меня есть два UIImageViews, в которые я хочу загрузить два разных изображения. У меня есть две кнопки, которые вызывают UIImagePickerController и вызывают мой

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

Метод.

Есть ли способ отличить, от какого сборщика вызывается указанный выше метод?

0
rockZ 28 Окт 2015 в 13:58

2 ответа

Лучший ответ

Вы можете создать экземпляр UIButton

@interface YourClass ()
{
   UIButton *_selectedButton;
}

И перед запуском UIImagePickerController вы можете сохранить кнопку в переменной экземпляра

-(void)button1Clicked:(UIButton *)button1{
      _selectedButton = button1;
     // call UIImagePickerController
} 

-(void)button2Clicked:(UIButton *)button2{
      _selectedButton = button2;
      // call UIImagePickerController
} 


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{   
      if (_selectedButton == button1){
         // perform your logic 
      } else if (_selectedButton == button2){
        // perform your logic 
      }
}
5
Piyush Sharma 28 Окт 2015 в 11:16

Поскольку я просто пытался ответить на тот же вопрос, я тоже опубликую свой ответ. Я не использовал отдельный UIButton для предоставления события клика для UIImageView. Вот мой полный код -

#import "ViewController.h"

#define IMAGE1 1
#define IMAGE2 2

@interface ViewController ()<UINavigationControllerDelegate ,UIImagePickerControllerDelegate,  UIGestureRecognizerDelegate, UIActionSheetDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *image1;
@property (weak, nonatomic) IBOutlet UIImageView *image2;
@property NSInteger selectedImage;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initImageView];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initImageView {

    _image2.userInteractionEnabled = YES;
    _image1.userInteractionEnabled = YES;

    UITapGestureRecognizer *image1Gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addPhoto:)];
    image1Gesture.numberOfTapsRequired = 1;
    image1Gesture.numberOfTouchesRequired = 1;
    [image1Gesture setDelegate:self];
    _image1.tag = IMAGE1;
    [_image1 addGestureRecognizer:image1Gesture];


    UITapGestureRecognizer *image2Gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addPhoto:)];
    image2Gesture.numberOfTapsRequired = 1;
    image2Gesture.numberOfTouchesRequired = 1;
    [image2Gesture setDelegate:self];
    _image2.tag = IMAGE2;
    [_image2 addGestureRecognizer:image2Gesture];
}


-(void)addPhoto:(UIGestureRecognizer*) gestureRecognizer
{
    NSLog(@"Tag = %ld",(long)[gestureRecognizer view].tag);

    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Select Source"
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                         destructiveButtonTitle:nil
                                              otherButtonTitles:@"Camera",
                            @"Gallery",nil];
    // Show the sheet
    if([gestureRecognizer view].tag == IMAGE1) {
        sheet.tag = IMAGE1;
    } else if([gestureRecognizer view].tag == IMAGE2) {
        sheet.tag = IMAGE2;
    } else {
        sheet.tag = 000;
    }

    [sheet showInView: self.view];
}

#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    _selectedImage = actionSheet.tag;
    switch (buttonIndex) {
        case 0:
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                [self performSelector:@selector(takePhoto) withObject:nil afterDelay:0.3];
            } else {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"The device have no camera" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];

            }
            break;

        case 1:
            [self performSelector:@selector(selectPhoto) withObject:nil afterDelay:0.3];
            break;

        case 2:
            NSLog(@"Invalid option");
            break;
    }
}


// take photo from camera
- (void)takePhoto {

    NSLog(@"takePhoto from camera");
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
    [self presentViewController:imagePicker animated:NO completion:nil];


}
// select photo from gallery
- (void)selectPhoto {

    NSLog(@"SelectPhoto from gallery");
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:imagePicker animated:NO completion:nil];
}


#pragma mark - UIImagePickerControllerDelegate delegate

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

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    [picker dismissViewControllerAnimated:YES completion:nil];

    if (_selectedImage == IMAGE1) {
        _image1.image = chosenImage;
    } else if (_selectedImage == IMAGE2) {
        _image2.image = chosenImage;
    } else {
        NSLog(@"Could not determine image selection");
    }
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return 1;
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:NO completion:nil];
}
@end
0
rockZ 28 Окт 2015 в 13:19