如何区分didFinishPickingMediaWithInfo中的UIImagePickerController
How to distuingish between UIImagePickerController in didFinishPickingMediaWithInfo
我有两个 UIImageViews
,我想在其中加载两个不同的图像。我有两个按钮触发 UIImagePickerController
并调用我的
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
方法。
有没有办法区分上面的方法调用的是哪个picker?
您可以创建 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
}
}
因为我只是在尝试同样的问题,所以我也会 post 我的答案。我没有使用单独的 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
我有两个 UIImageViews
,我想在其中加载两个不同的图像。我有两个按钮触发 UIImagePickerController
并调用我的
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
方法。
有没有办法区分上面的方法调用的是哪个picker?
您可以创建 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
}
}
因为我只是在尝试同样的问题,所以我也会 post 我的答案。我没有使用单独的 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