2012-09-05 85 views
1

我有一個UIViewController,我想在加載視圖時打開相機的UIImagePickerController。當視圖加載時打開UIImagePickerController

答:我寫了下面的: 編輯 - 我現在使用viewDidAppear

- (void)viewDidAppear:(BOOL)animated{ 
    imagePicker = [[UIImagePickerController alloc] init]; 
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){ 
     [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera]; 
    } 
    else{ 
     [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 
    } 
    [imagePicker setDelegate:self]; 
    [self presentModalViewController:imagePicker animated:YES]; 
} 

的問題是,用戶拍攝快照或從文件夾中選擇圖像後 - 它不斷加載UIImagePicker再次 - 我怎麼才能讓它只調用一次?

B.我想將相機顯示在一個矩形內(可以是UIImage或UIView)而不是全屏,這樣我就可以離開我的頂部導航欄。

我怎樣才能達到上述目的?

+0

不能與圖像拾取做到這一點,你必須用相機作爲輸入設備創建捕獲會話,並在自定義視圖中顯示它。HTTP ://www.musicalgeometry.com/?p = 1273 –

+0

我不能做A或B? – Dejell

+0

兩種都有。對於「A」我建議你在viewDidAppear void wait_fence錯誤,並且使用圖像選擇器無法完成「B」。 –

回答

1

對於情況A,更好地把這個viewDidAppear,而不是viewWillAppear避免動畫碰撞(和wait_fences錯誤)

在B的情況,您可能需要捕獲會話,或者需要使用轉換到tweek的UIImagePickerController。你可以做到這一點得益於後續UIImagePickerController屬性:

  • cameraViewTransform更改應用於顯示了相機/攝像視圖的CGAffineTransform。因此,你將能夠將其縮小例如
  • showsCameraControls,你可以設置爲NO隱藏默認iOS的控制,如果你打算添加自己的UIButtons代替
  • cameraOverlayView,可以讓你把一個任意UIView頂上相機視圖。您通常使用的視圖有一個backgroundColor = [UIColor clearColor]並添加一些子視圖,尤其是一些裝飾UIImageView(如果需要)(例如一幀),一些UIButton s觸發相機(並使用UIImagePickerControllerUIImagePickerController的方法UIImagePickerController相應IBAction如需要),並取代你與以前的財產藏集結其攝像頭的控制,等等

在你的情況,如果你想保持普通相機的按鈕,但只是想改變框架和使其不是全屏,那麼cameraTransform屬性通常就是你需要的。添加裝飾框周圍將簡單地包括使用UIImageViewcameraOverlayView財產,並用它來顯示裝飾框的圖像(圖像與它的中心透明的,使得相機視圖是當然的直通它可見,)


[編輯]請注意(根據文檔)cameraTransform屬性只有在拍攝動畫時纔可用(當時sourceType == UIImagePickerControllerSourceTypeCamera)。因此,您可以使用它來讓用戶從其相機拍攝照片,但如果您想使用UIImagePickerController從圖庫中選擇一張照片,則不能使用cameraTransform(這很合理,因爲它看起來很奇怪在屏幕上顯示)。所以一定要在這種情況下才使用它,特別是在設備上沒有UIImagePickerControllerSourceTypeCamera信號源類型時避免它。

+0

viewWillApepar不斷加載它不停 - 用戶拍攝照片或選擇一個從庫後,它再次加載它 – Dejell

+0

當然,你必須使用一些條件。通常情況下,一旦用戶捕獲了圖像,你可能會把它保存在你的'UIViewController'的'@property UIImage * chosenImage'中,所以你可以使用if(self.chosenImage == nil)'作爲條件來顯示'viewDidAppear'中的'UIImagePickerController';) – AliSoftware

+0

謝謝。關於cameraOverlayView - 你有一個代碼示例如何做到這一點? – Dejell

0
- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
    [self getMediaFromSource:UIImagePickerControllerSourceTypeCamera]; 

} 
- (IBAction)TakePhoto:(id)sender { 
    [self getMediaFromSource:UIImagePickerControllerSourceTypeCamera]; 
} 

- (void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType{ 
    UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
    picker.delegate   = self; 
    picker.allowsEditing  = NO; 
    picker.sourceType   = sourceType; 
    [self presentModalViewController:picker animated:YES]; 

    [picker release]; 
} 

#pragma mark - UIImagePickerController delegate methods 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 
    [picker dismissModalViewControllerAnimated:YES]; 
    [self.tabBarController setSelectedIndex:1]; 
} 
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 

    [picker dismissModalViewControllerAnimated:YES]; 
    [self.tabBarController setSelectedIndex:1]; 
} 

更新了B部分。cameraOverlayView

- (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) - 
            self.view.frame.size.height - 10.0, 
            CGRectGetWidth(overlayViewFrame), 
            self.view.frame.size.height + 10.0); 
     self.view.frame = newFrame; 
     [self.imagePickerController.cameraOverlayView addSubview:self.view]; 
    } 
} 
} 

http://developer.apple.com/library/ios/#samplecode/PhotoPicker/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010196

我建議用「cameraOverlayView,」試圖建立某種形式的僞導航欄在你預期的相互作用將努力打 示例代碼。

「您可以自定義圖像選擇器控制器以自行管理用戶交互。爲此,請提供包含要顯示的控件的疊加視圖,並使用」捕獲靜止圖像或電影「中所述的方法。除了默認控件外,或者不是默認控件,顯示你的自定義覆蓋視圖,UIImagePickerController類的自定義覆蓋視圖可以在iOS 3.1及更高版本中通過cameraOverlayView屬性獲得。有關代碼示例,請參閱PhotoPicker示例代碼項目。

重要UIImagePickerController類僅支持縱向模式,該類用於原樣使用,不支持子類。此類的視圖層次結構是私有的,不能修改,只能使用一個例外。您可以將自定義視圖的cameraOverlayView屬性,並使用該視圖來顯示其他信息或管理的互動拍照界面和代碼之間。」

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html

+0

您的解決方案適用於A。但B怎麼辦? – Dejell

+0

所以我不確定pickercontrol可以做你要求的B.請參閱上面的更新。 –

1

只實現你的行動統一到viewDidLoad方法。這是我做的,現在可完全正常。

相關問題