2013-02-17 171 views
3

我有一個問題,即:不叫(委託設置爲self)的UIImagePickerController委託方法

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

只能被稱爲用相機,而不是當我試圖通過用戶的瀏覽照片,選擇一個圖像。

這裏是爲UIImagePicker代表我的初始化代碼:

-(void)viewDidLoad{ 

    [self initImagePicker]; 

} 


-(void)initImagePicker{ 
    DLog(@""); 

    self.imagePickerController.delegate = self; 

} 



- (void)setupImagePicker:(UIImagePickerControllerSourceType)sourceType 
{ 
    self.imagePickerController.sourceType = sourceType; 

    if (sourceType == UIImagePickerControllerSourceTypeCamera) 
    { 
     // user wants to use the camera interface 
     // 
     self.imagePickerController.showsCameraControls = NO; 

     if ([[self.imagePickerController.cameraOverlayView subviews] count] == 0) 
     { 
      // setup our custom overlay view for the camera 
      // 
      // ensure that our custom view's frame fits within the parent frame 
      CGRect overlayViewFrame = self.imagePickerController.cameraOverlayView.frame; 

      CGRect newFrame = CGRectMake(0.0,CGRectGetHeight(overlayViewFrame)-15.0,CGRectGetWidth(overlayViewFrame),15.0); 

      self.view.frame = newFrame; 
      [self.imagePickerController.cameraOverlayView addSubview:self.view]; 
      [self.view bringSubviewToFront:self.view]; 
     } 
    } 
} 

然後在我的根類,一來就是授人以類UIImagePicker是委託給UIImagePicker:

#pragma mark - 
#pragma mark Toolbar Actions 

- (void)showImagePicker:(UIImagePickerControllerSourceType)sourceType 
{ 




    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
    { 
     // camera is not on this device, don't show the camera button 
     NSMutableArray *toolbarItems = [NSMutableArray arrayWithCapacity:self.myToolbar.items.count]; 
     [toolbarItems addObjectsFromArray:self.myToolbar.items]; 
     [toolbarItems removeObjectAtIndex:2]; 
     [self.myToolbar setItems:toolbarItems animated:NO]; 
    } 

    if ([UIImagePickerController isSourceTypeAvailable:sourceType]) 
    { 


     self.overlayViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"OverlayViewController"]; 
     self.overlayViewController.delegate = self; 

     self.overlayViewController.imagePickerController = [[UIImagePickerController alloc] init]; 
     [self.overlayViewController setupImagePicker:sourceType]; 
     [self presentModalViewController:self.overlayViewController.imagePickerController animated:YES]; 
    } 
} 

我知道這是一個SO問題的很多代碼,但對於實現這個類的經驗豐富的人來說,我做錯了什麼?

對我來說,這條線是奇怪的:

self.overlayViewController.imagePickerController = [[UIImagePickerController alloc] init]; 

我從來沒有分配和初始化前的所有者以外的財產。這是問題嗎?

+0

從哪裏調用'setupImagePicker'和'showImagePicker' – 2013-02-17 12:05:23

+0

在調用showImagePicker時出現的UIImagePicker委託視圖控制器中調用setUpImagePicker。 – stackOverFlew 2013-02-17 12:07:14

回答

1

找到修復:

需要補充:

self.overlayViewController.imagePickerController.delegate = self.overlayViewController; 

後:

self.overlayViewController.imagePickerController = [[UIImagePickerController alloc] init]; 
+0

stackOverflow你做了同樣的事情@tobiasbayer建議你以前做,但你沒有標記他的答案是正確的!偉大的方式來賺取積分! – 2017-01-19 05:11:10

6

我認爲你設置的代表太早(initImagePickerviewDidLoad)。 self.imagePickerController可能沒有。 您應該在創建UIImagePickerController之後設置委託。

+0

!!大聲笑你可能打敗了我。我剛剛發佈了我的答案,請接受修改並接受您的回答。 – stackOverFlew 2013-02-17 12:05:01

1

集UIImagePickerDelegate和UINavigationControllerDelegate中的.h文件中與@interface。

@interface class : UIView <UIImagePickerDelegate , UINavigationControllerDelegate> 

用的UIImagePickerController對象設置自委託過,

imgPickerController.delegate = self; 

希望它可以幫助你。