为UIImagePickerController设置一个等价的标签

时间:2013-07-24 12:15:01

标签: ios cocoa-touch uiimagepickercontroller

我有一个名为imagePicker的UIImagePickerController

UIImagePickerController * imagePicker;

我正在使用方法

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo

我需要知道正在使用哪个imagePicker,例如,您可以为其他人设置标签,例如UIAlertView等,它们是UIImagePickerController的等价物吗?或者我如何判断哪个被调用。

4 个答案:

答案 0 :(得分:10)

您可以使用:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
   if(picker == _imagePicker)
       //do your task
}

或设置标签:

imagePicker.view.tag = 7;

然后检查:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
   if(picker.view.tag == 7)
       //do your task
}

答案 1 :(得分:1)

保留对您拥有的不同图像选择器的引用作为属性。然后只需比较它们的指针值:

if (picker == self.picker1) {
    // ...
} else if (picker == self.picker2) {
    // ...
} ...

完成拾取器后,将其属性设置为nil以允许清除它们。

如果您确实要“标记”它们,可以使用Objective-C associative references

答案 2 :(得分:0)

我会将imagepicker的实例保存到ivar或property。在你检查它是否是同一个实例之后。

@interface MyImagePicker : UIViewController
{
    UIImagePickerController * _ imagePicker;
}   

@implementation 
- (void)showImagePicker {
   _imagePicker = [[UIImagePickerController alloc] init];
   //config and show
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
if(picker == _imagePicker) {
  //My picker do stuff here
}

答案 3 :(得分:0)

要区分具有相同UIImagePickerViewController的{​​{1}},您可以引入新实体sourceType

Role

此外,在您的fileprivate enum ImagePickerRole { case avatar case chooseImage case takePhoto } fileprivate class ImagePicker: UIImagePickerController { private(set) var role: ImagePickerRole = .takePhoto } fileprivate extension ImagePicker { convenience init(role imagePickerRole: ImagePickerRole) { self.init() role = imagePickerRole switch role { case .avatar, .takePhoto: sourceType = .camera cameraCaptureMode = .photo showsCameraControls = true case .chooseImage: sourceType = .photoLibrary } } 中,您可以检查控制器的角色:

UIImagePickerViewControllerDelegate