2012-07-20 85 views
0

我有一個按鈕,當用戶按下它時,他被導航到相機拍照。iOS:嘗試在應用程序中使用相機時,View Controller崩潰。

當他拍攝照片時,他按下按鈕並返回控制器。但是,當這種情況發生時,導航欄會跳到屏幕上,低於電話所在的電池/信號欄。奇怪的是,這發生在4/4S,但不能在3GS

+1

你能不能張貼一些代碼..... – WaaleedKhan 2012-07-20 10:23:01

+0

不是因爲它的一個巨大的項目..我只是問一般什麼可能出錯了,看到這樣一個奇怪的行爲 – user1511244 2012-07-20 10:29:27

+0

我從來沒有說我需要你的項目。我說你的代碼對於我們來說很容易看到你做了什麼而不是猜測。無論如何看看這個代碼從Apple Reference [鏈接](http://developer.apple.com/library/ios/#samplecode/PhotoPicker/Listings/ReadMe_txt.html) – WaaleedKhan 2012-07-20 10:34:04

回答

1

這是相當艱難的回答這個問題不知道多一點細節,但這裏的一些代碼,我用它來調出相機,拍下了照片,然後成功關閉相機。該方法可以通過一行代碼來調用:[self takePhoto];

- (void) takePhoto { 

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
     //Clear out the UI first 

     UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 

     picker.delegate = self; 

     picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
     //picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; -> use this if you want them to select from the library 
     [self presentViewController:picker animated:YES completion:nil]; 
    } else { 
     //Device does not support a camera, show a message if desired 
    } 

} 

然後你需要爲這一切讓程序知道拍攝圖像或選擇,以及如何關閉時該怎麼辦的委託,這就是這代碼:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    self.workingImage = nil; 

    //I grab the image and store globally, but first I manually scale and rotate it if necessary  
    self.workingImage = [self scaleAndRotateImage:[info objectForKey:UIImagePickerControllerOriginalImage]]; 

    //I display the image in my UI view, so this chunk of code will size it properly 
    CGRect bound; 
    bound.origin = CGPointZero; 
    bound.size = img.size; 
    imageView.bounds = bound; 

    //Set the UI image view and dismiss the controller 
    [imageView setImage:self.workingImage]; 
    [picker dismissModalViewControllerAnimated:YES]; 

} 

顯然,要確保你的控制器的.h正確實現委託像這樣:

@interface ViewController : UIViewController<UIImagePickerControllerDelegate> { ... } 

希望這有助於?

相關問題