2015-05-29 70 views
1

我有一個UIImagePickerController自定義覆蓋。我試圖實現取消按鈕,將彈出UIImagePickerController。我的代碼設置如下:無法彈出UIImagePickerController

- (void)tapPhoto { 

    //setup the picker 
    self.picker = [[UIImagePickerController alloc] init]; 
    self.picker.delegate = self; 
    self.picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
    self.picker.cameraDevice=UIImagePickerControllerCameraDeviceFront; 
    self.picker.showsCameraControls=NO; 

    self.cameraOverlay = [[CameraOverlay alloc] init]; 
    UIImage *camoverlayImage = self.cameraOverlay.cameraOverlayImage; 
    UIImageView *camoverlayImageView = [[UIImageView alloc] initWithImage:camoverlayImage]; 
    camoverlayImageView.frame=CGRectMake(0, 0, self.cameraOverlay.previewSize.width , self.cameraOverlay.previewSize.height); 

    //init the control view 
    UIView *cameraControls= [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)]; 
    [cameraControls addSubview:camoverlayImageView]; 

    //Shutter Button 
    UIButton *button = [[UIButton alloc] init];//70*70 
    [button setBackgroundImage:[UIImage imageNamed:@"cameraoff"] forState:UIControlStateNormal]; 
    [button setBackgroundImage:[UIImage imageNamed:@"cameraon"] forState:UIControlStateHighlighted]; 
    button.frame = CGRectMake(([[UIScreen mainScreen] bounds].size.width/2)-70/2, [[UIScreen mainScreen] bounds].size.height-70-10, 70 , 70); 
    [button addTarget:self action:@selector(takePhoto) forControlEvents:UIControlEventTouchUpInside]; 
    [cameraControls addSubview:button]; 


    UIButton *button2 = [[UIButton alloc] init]; 
    [button2 setTitle:@"Cancel" forState:UIControlStateNormal]; 
    [button2 addTarget:self action:@selector(cancelPhoto) forControlEvents:UIControlEventTouchUpInside]; 
    button2.frame = CGRectMake(5, [[UIScreen mainScreen]bounds].size.height-30, 0, 0); 
    [button2 sizeToFit]; 
    [cameraControls addSubview:button2]; 

    [self.picker setCameraOverlayView:cameraControls]; 
    [self presentViewController:self.picker animated:YES completion:NULL]; 

} 

-(void)cancelPhoto{ 

    NSLog(@"photo canceled"); 
    [self.navigationController pushViewController:self.picker animated:YES]; 



} 

-(void)takePhoto{ 
    //this will auto trigger the control 
    [self.picker takePicture]; 
} 

我想從導航控制器彈出它,但沒有任何運氣。我也試過[self.picker popViewControllerAnimated:YES];。任何人都可以給我任何指示爲什麼這不起作用?由於

回答

3

您是(正確地)出示您的選擇器:

[self presentViewController:self.picker animated:YES completion:NULL]; 

present反面不是推或流行,但dismiss

[self dismissViewControllerAnimated:YES completion:nil]; 
1

首先這條線是錯誤的!

[self.navigationController pushViewController:self.picker animated:YES]; 

你爲什麼推選擇器,當你選擇器當前視圖控制器已經出現

正確的方法是這樣的: -

-(void)cancelPhoto{ 

     //as you have presented the view controller so have dismiss it by this way. 
    [self.picker dismissViewControllerAnimated:YES completion:nil]; 

} 
0

嘗試像這個 -

-(void)tapPhoto { 
    //set up the Picker 
    self.picker = [[UIImagePickerController alloc] init]; 
    self.picker.delegate = self; 
    self.picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
    self.picker.cameraDevice=UIImagePickerControllerCameraDeviceFront; 

    //add a UIView as CameraOverLay 
     UIView *overlayView = [[UIView alloc] initWithFrame:picker.view.frame]; 
    [self.picker setCameraOverlayView:overlayView]; 

    //Present the Picker 
    [self presentViewController:picker animated:YES completion:nil]; 
} 

這樣,默認情況下你應該有一個取消按鈕。然後只需實現委託方法並在點擊取消按鈕時關閉控制器。

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

     //get the captured image if any 
    if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) 
      == kCFCompareEqualTo){ 
     UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 

     //save the image in photo library 
     UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); 
    } 

    //pop out or remove the picker 
    [picker dismissViewControllerAnimated:YES completion:nil]; 
    } 
0

請勿嘗試使用popViewControllerAnimated。請嘗試:

[self.picker dismissViewControllerAnimated:YES completion:nil];