2013-05-09 17 views
0

我正在編寫一個應用,該應用具有用於使用相機拍攝照片的自定義視圖,類似於Apple的AVCam。其中,每次切換相機時,我都想讓按鈕消失並重新出現在閃光燈圖標上。 IE使用前置攝像頭時,閃光燈按鈕不應存在,使用背面時應該!檢查iOS中當前正在使用哪個相機應用

我給這家目前的代碼是:

AVCaptureDevicePosition position = [[videoInput device] position]; 

    if (position == AVCaptureDevicePositionBack) { 
    self.flashButton.hidden == YES; 
} 

但它在videoInput的錯誤出現,我不知道爲什麼...任何文檔,你可以指點我或想法我的代碼中的更改將非常感謝!

編輯

基本上只是具體爲什麼它拿出這個代碼「使用未聲明的標識符的」錯誤:

回答

1

下面的代碼可以幫助你:

AVCaptureDeviceInput *newVideoInput; 
AVCaptureDevicePosition currentCameraPosition = [[videoInput device] position]; 

if (currentCameraPosition == AVCaptureDevicePositionBack) 
{ 
    currentCameraPosition = AVCaptureDevicePositionFront; 
} 
else 
{ 
    currentCameraPosition = AVCaptureDevicePositionBack; 
} 

AVCaptureDevice *backFacingCamera = nil; 
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; 
for (AVCaptureDevice *device in devices) 
{ 
    if ([device position] == currentCameraPosition) 
    { 
     backFacingCamera = device; 
    } 
} 
newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:backFacingCamera error:&error]; 
+0

感謝您的幫助,但我在AVCam應用程序中看到了這一點。我不知道如何在僞代碼中編寫一小段代碼,例如:if current camera = back camera {show button} else {hide button} – falky 2013-05-09 11:43:54

+0

每次使用[videoInput device]時都會出現錯誤。所以我想知道是否有任何想法或任何其他方式來確定我的UIView使用哪個相機('AVCaptureDevicePositionBack'或'AVCaptureDevicePositionFront')。從那裏我會隱藏或顯示flashButton ...?儘管感謝您的幫助。我的想法是這樣的:'AVCaptureDevicePosition currentCameraPosition = [[videoInput device] position]; if([currentCameraPosition isEqual:AVCaptureDevicePositionBack]){self.flashButton.hidden = NO;} else {self.flashButton.hidden = YES; }」 – falky 2013-05-09 12:12:16

0

我一直在尋找類似的問題的解決方案,並提出了這個問題,它可能適合你(僅在iOS8測試並寫入SWIFT):

var captureDevice : AVCaptureDevice? 

... 

var currentDevice:String = captureDevice?.localizedName as String! 

if currentDevice.rangeOfString("Back Camera") != nil { 
    //hide flash icon 
} else if currentDevice.rangeOfString("Front Camera") != nil { 
    //show flash icon 
} 

此代碼假定你已經設置相機正確

注:這可能不是最好的辦法,因爲如果蘋果決定改變localizedName這將打破。而且,我知道這個問題很古老,但它可能會幫助其他人絆倒它

相關問題