2014-05-11 70 views
0

我正在嘗試使用自定義cameraOverlayView編程UIImagePickerController。當我撥打UIImagePickerControllertakePicture方法時,UIView會變爲白色屏幕而不是選擇器,您可以在其中選擇「使用照片」。我怎樣才能將後續的UIView設置爲拾取器而不是空白UIViewUIImagePickerController在調用takePicture方法後進入白色屏幕

我已包括下面我的代碼:

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(applicationDidBecomeActive) 
              name:UIApplicationDidBecomeActiveNotification 
              object:nil]; 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 


-(void)dealloc 
{ 
[[NSNotificationCenter defaultCenter] removeObserver:self 
               name:UIApplicationDidBecomeActiveNotification 
               object:nil]; 
} 

- (void)applicationDidBecomeActive 
{ 




//Start the camera up 

_imageViewPickerController = [[UIImagePickerController alloc] init]; 

//Hide default UI elements 

_imageViewPickerController.sourceType = UIImagePickerControllerSourceTypeCamera; 
_imageViewPickerController.showsCameraControls = NO; 
_imageViewPickerController.navigationBarHidden = YES; 
_imageViewPickerController.toolbarHidden = YES; 

//Start the button overlay 

UIView *btnView = [[UIView alloc] initWithFrame:_imageViewPickerController.view.bounds]; 
btnView.opaque = NO; 
btnView.clipsToBounds = YES; 

//Start up the button 
UIButton *snapButton = [[UIButton alloc] initWithFrame:CGRectMake(-6, 504, 65, 65)]; 
snapButton.alpha = 0.5; 
snapButton.transform = CGAffineTransformMakeRotation(DEGREES_RADIANS(90)); 
[snapButton addTarget:self action:@selector(snapThat) forControlEvents:UIControlEventTouchUpInside]; 

//Set the button's picture 
UIImage *snapButtonImage = [UIImage imageNamed:@"Button.png"]; 
[snapButton setImage:snapButtonImage forState:UIControlStateNormal]; 

//Add button to the overlay 
[btnView addSubview:snapButton]; 

//Overlay button view 

_imageViewPickerController.cameraOverlayView = btnView; 

//Fix for iPhone 5 and make fullscreen 

CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, -55.0); 
CGAffineTransform scale = CGAffineTransformMakeScale(1.333333, 1.333333); 
CGAffineTransform rotate = CGAffineTransformMakeRotation(DEGREES_RADIANS(180)); 
CGAffineTransform transform = CGAffineTransformConcat(translate, scale); 
transform = CGAffineTransformConcat(transform, rotate); 
_imageViewPickerController.cameraViewTransform = transform; 

//Let's present it all 

[self presentViewController:_imageViewPickerController 
        animated:NO 
       completion:NULL]; 


} 

- (void)snapThat { 
[_imageViewPickerController takePicture]; 
} 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
// Save photo 
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) 
{ 
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
    UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil); 
} 
[picker dismissViewControllerAnimated:YES completion:NULL]; 
} 

編輯:我修改我的代碼,包括這一點,我仍然得到空白的UIView: [self presentViewController:_imageViewPickerController animated:NO completion:NULL]; 不應該這樣保存的圖片和回報我到相機視圖?

回答

0

當我調用我的UIImagePickerController的takePicture方法時,UIView更改爲白色屏幕而不是選擇器,您可以在其中選擇「使用照片」。

它這樣做,因爲你didFinishPickingMediaWithInfo:實現調用dismissViewControllerAnimated:。如果您不希望相機界面被解僱,請不要解僱它。

你沒有得到任何自動的第二個視圖(你可以選擇「使用照片」或其他)。如果你想這樣做,你必須創建你自己的第二個視圖控制器,並將它推到UIImagePickerController(這是一個導航控制器)上。

+0

我在這裏解釋更多關於這個:http://www.apeth.com/iOSBook/ch30.html#_using_the_camera – matt

+0

這裏是一個可下載的例子,我創建了第二個視圖控制器並將其推到UIImagePickerController上,以便用戶可以決定是否使用這個圖像或不:https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch17p704takeAPicture2/ch30p962takeAPicture2/ViewController.m – matt

+0

我已經修改了關於我的問題的OP 。看來我的代碼應該都可以工作。如果你不介意進一步澄清,我會非常感激。 – mattchue