2013-07-24 60 views
1

我有一個名爲的UIImagePickerController imagePicker設置標籤等效的UIImagePickerController

UIImagePickerController * imagePicker; 

進出口使用的方法

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

,我需要知道正在使用imagePicker,例如,你可以設置其他人的標籤,如UIAlertView等,是他們相當於UIImagePickerController?或者我將如何分辨哪個被調用。

回答

8

您可以使用:

-(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

保留對不同圖像選擇器的引用,使其具有屬性。然後比較它們的指針值:

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

當您完成採集器時,將它們的屬性設置爲零以允許它們被清除。

如果你真的想'標記'他們,你可以使用Objective-C associative references

0

我會節省imagepicker到伊瓦爾或財產的實例。在你檢查它是否是同一個實例之後。

@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 
} 
0

再以同樣的sourceTypeUIImagePickerViewController的區別,你可以引入一個新的實體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 
    } 
}