1

我有一个名为 imagePicker 的 UIImagePickerController

UIImagePickerController * imagePicker;

我正在使用该方法

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

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

4

4 回答 4

11

您可以使用:

-(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
}
于 2013-07-24T12:22:01.300 回答
1

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

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

完成选择器后,将它们的属性设置为 nil 以允许清理它们。

如果您真的想“标记”它们,可以使用Objective-C 关联引用

于 2013-07-24T12:19:53.930 回答
0

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

@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
}
于 2013-07-24T12:25:06.937 回答
0

要区分UIImagePickerViewController's 与相同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可以检查控制器的角色:

public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    guard let imagePicker = picker as? ImagePicker else { return }

    switch imagePicker.role {
    case .avatar:
        // Your code here
    case .takePhoto:
        // Your code here
    case .chooseImage:
        // Your code here
    }
}
于 2017-12-14T15:12:54.190 回答