2013-09-21 61 views
8

由於某種原因,第一次在我的應用程序中以相機模式打開UIImagePickerController時,它出現空白。我必須關閉並重新打開該視圖才能使相機饋送開始工作。我正在使用適用於iOS 6的標準代碼來拍攝相機。從下面的示例中我發射了capturePhoto:方法。任何其他人都會用iOS 7相機跑進這種輕鬆的世界?我檢查了蘋果開發論壇,但幾乎不可能在那裏找到答案。iOS 7 UIImagePickerController相機沒有圖像

- (IBAction)capturePhoto:(id)sender { 
    [self doImagePickerForType:UIImagePickerControllerSourceTypeCamera]; 
} 

- (void)doImagePickerForType:(UIImagePickerControllerSourceType)type { 
    if (!_imagePicker) { 
     _imagePicker = [[UIImagePickerController alloc] init]; 
     _imagePicker.mediaTypes = @[(NSString*)kUTTypeImage]; 
     _imagePicker.delegate = self; 
    } 
    _imagePicker.sourceType = type; 
    [self presentViewController:_imagePicker animated:YES completion:nil]; 
} 

why so empty?

回答

16

我也在使用UIImagePickerController,並遇到空白屏幕的相同問題。我想就klaudz提到的有關iOS 7授權攝像頭的內容做一點點擴展。

參考: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVCaptureDevice_Class/Reference/Reference.html

「錄製音頻總是需要來自用戶的明確許可,錄像還要求在某些地區銷售的設備的用戶權限。」

下面是一些代碼片段,您可以先檢查以確定您是否有相機權限,並在您的應用程序之前未請求過相機時請求它。如果您因較早的請求而被拒絕,您的應用可能需要向用戶發出通知,以進入設置,以便像klaudz指出的那樣手動啓用訪問。

的iOS 7例如

NSString *mediaType = AVMediaTypeVideo; // Or AVMediaTypeAudio 

AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType]; 

// This status is normally not visible—the AVCaptureDevice class methods for discovering devices do not return devices the user is restricted from accessing. 
if(authStatus == AVAuthorizationStatusRestricted){ 
    NSLog(@"Restricted"); 
} 

// The user has explicitly denied permission for media capture. 
else if(authStatus == AVAuthorizationStatusDenied){ 
    NSLog(@"Denied"); 
} 

// The user has explicitly granted permission for media capture, or explicit user permission is not necessary for the media type in question. 
else if(authStatus == AVAuthorizationStatusAuthorized){ 
    NSLog(@"Authorized"); 
} 

// Explicit user permission is required for media capture, but the user has not yet granted or denied such permission. 
else if(authStatus == AVAuthorizationStatusNotDetermined){ 

    [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) { 

     // Make sure we execute our code on the main thread so we can update the UI immediately. 
     // 
     // See documentation for ABAddressBookRequestAccessWithCompletion where it says 
     // "The completion handler is called on an arbitrary queue." 
     // 
     // Though there is no similar mention for requestAccessForMediaType, it appears it does 
     // the same thing. 
     // 
     dispatch_async(dispatch_get_main_queue(), ^{ 

      if(granted){ 
       // UI updates as needed 
       NSLog(@"Granted access to %@", mediaType); 
      } 
      else { 
       // UI updates as needed 
       NSLog(@"Not granted access to %@", mediaType); 
      } 
     }); 

    }]; 

} 

else { 
    NSLog(@"Unknown authorization status"); 
} 
+0

如何在iOS 6中做到這一點? – jianpx

+0

我在捕獲圖像時也遇到了問題。我該如何解決它。 – Nirav

+0

在哪裏使用這個解決方案? – Nirav

7

在iOS系統7中,一個應用程序可以得到授權的用戶的前訪問攝像機。 當應用第一次訪問攝像頭時,iOS會顯示一個警報視圖來詢問用戶。 用戶也可以在Settings--Privacy--Camera--[Your app's name]中設置授權。

如果開關關閉,相機將保持黑色空白視圖。

  1. 如果你調用攝像頭使用AVCaptureDeviceInput,你可以檢查,如:

    NSError *inputError = nil; 
    AVCaptureDeviceInput *captureInput = 
        [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&inputError]; 
    if (inputError && 
        inputError.code == AVErrorApplicationIsNotAuthorizedToUseDevice) 
    { 
        // not authorized 
    } 
    
  2. 如果使用UIImagePickerController打電話,我仍然在尋找一種方式來檢查是否得到了授權。

    我嘗試這兩種方法:

    [UIImagePickerController isSourceTypeAvailable:] 
    [UIImagePickerController isCameraDeviceAvailable:] 
    

    但我以前不認爲他們全都是返回工作。

UPDATE

感謝斯科特的擴張。 [AVCaptureDevice authorizationStatusForMediaType:]是一個更好的檢查方法。

AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; 
if (authStatus == AVAuthorizationStatusAuthorized) { 
    // successful 
} else { 
    // failed, such as 
    // AVAuthorizationStatusNotDetermined 
    // AVAuthorizationStatusRestricted 
    // AVAuthorizationStatusNotDetermined 
} 

但記得要檢查iOS版本,因爲[AVCaptureDevice authorizationStatusForMediaType:]AVAuthorizationStatus可用上述的iOS 7

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { 
    // code for AVCaptureDevice auth checking 
} 
+0

在iOS6中使用什麼授權....你的第一點是在條件使用'AVErrorApplicationIsNotAuthorizedToUseDevice'? –

+1

iOS6隱私設置中沒有任何設置,因此如果需要,只需使用'[UIImagePickerController isSourceTypeAvailable:]'和' [UIImagePickerController isCameraDeviceAvailable:]'進行檢查。 – klaudz

+3

而不是'[[[UIDevice currentDevice] systemVersion] floatValue]> = 7.0'你應該檢查'[AVCaptureDevice respondsToSelector:@selector(authorizationStatusForMediaType :)]' – user102008

4

我經歷了完全相同的問題,並試圖用沒有運氣在互聯網上每一個解決方案。但最後我發現這是後臺線程阻止相機預覽顯示。如果您像我一樣嘗試打開相機時碰巧有後臺線程在運行,請嘗試阻止後臺線程並查看會發生什麼情況。希望你能解決它。

+0

這似乎是可能的罪魁禍首,我會挖掘我的代碼,試着解決這個問題。謝謝回覆。 – KyleStew

+0

這裏有什麼進展?我爲蘋果派發了一個TSI,但沒有迴應,從我的應用程序中刪除GCD是不可能的,我只是希望有一種方法可以暫停或停止GCD以顯示相機,如果這是遠程真實的=( –

+0

我還沒有能夠擊中在事件的確切步驟中重複它一貫,我的代碼庫中做的事似乎已經解決了問題,因爲我不再看到這個問題,我希望你能夠把它排除。 – KyleStew

0

我碰到這種控制AQPhotoPicker。這是很容易使用,並希望它會幫助你

相關問題