2013-05-08 35 views
1

我試圖顯示從被加入作爲原始視圖控制器的子視圖一個程序生成的UIView一個UIImagePicker。圖像選擇器出現,相機也出現了,但功能中斷,沒有任何適當的導航。我如何正確加載圖像選擇器從UIView而不是UIControlView?IOS UIImagePicker在一個UIView子視圖

碼打破:

- (void)captureImage:(id)sender 
{ 
    [[[UIActionSheet alloc] initWithTitle:nil 
           delegate:self 
         cancelButtonTitle:@"Close" 
        destructiveButtonTitle:nil 
         otherButtonTitles:@"Take photo", @"Camera Roll", nil] 
    showInView:self]; 
} 

//UIImagePicker specific methods 
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    switch (buttonIndex) 
    { 
     case 0: 
      [self takePhoto]; 
       break; 

     case 1: 
      [self fromCameraRoll]; 
       break; 
    } 
} 

-(void)takePhoto 
{ 
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES) 
    { 
     // Create image picker controller 
     UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 

     // Set source to the camera 
     imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; 

     // Delegate is self 
     imagePicker.delegate = self; 

     // Show image picker 
     [self addSubview:imagePicker.view]; 
    } 
} 

-(void)fromCameraRoll 
{ 
    UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init]; 
    imgPicker.delegate = self; 
    imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 

    [self addSubview:imgPicker.view]; 
} 

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    // Access the uncropped image from info dictionary 
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 

    //do something with the image 

    //add code to pick images here and store them in the preview 
    [picker dismissModalViewControllerAnimated:NO]; 
} 

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{ 
    [picker dismissModalViewControllerAnimated:NO]; 
} 
+0

通過iPhone,你應該模態呈現影像選擇器(全屏)使用presentViewController:動畫:完成:。我不知道蘋果是否會贊同從較小的角度提出它。 – rdelmar 2013-05-08 17:31:38

回答

2

的UIImagePickerController是視圖控制器類,你最好通過電話委託或類似的東西與覆蓋子視圖實現自己的目標,出示您的影像選擇器到父視圖控制器。

UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init]; 
imgPicker.delegate = self; 
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 

CustomOverlayView *overlayview = [[CustomOverlayView alloc] init]; 

imgPicker.cameraOverlayView = overlayview; 

[self presentModelViewController:imgPicker animated:YES]; 

在覆蓋類添加另一位代表解僱你的相機選擇器控制器視圖

[imgPicker dismissModelViewControllerAnimated:YES]; 
+1

只是一個簡短的說明,更好地使用[自我presentViewController:imagePicker動畫:是完成:無];和[picker dismissViewControllerAnimated:YES completion:nil]; 因爲presentModelViewController和dismissModelViewControllerAnimated已被棄用 – 2014-03-26 10:48:48