问题
let VideoDevice = CameraWithPosition(AVCaptureDevicePosition.Back) // not working
let VideoDevice = CameraWithPosition(AVCaptureDevicePosition.Front) // working
if let stillOutput = self.stillImageOutput {
if let videoConnection = stillOutput.connectionWithMediaType(AVMediaTypeVideo)
{
println("stillOutput \(stillOutput)")
stillOutput.captureStillImageAsynchronouslyFromConnection(videoConnection){
(imageSampleBuffer : CMSampleBuffer!, _) in
println("imageSampleBuffer \(imageSampleBuffer)") //prints nil for back camera, works for front camera
...more code
I am able to capture image from Front Camera but same process not working for Back Camera of my Iphone, Is there any different settings for both camera?
Receiving imageSampleBuffer as nil for Back camera..
error log :
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x1704682c0 {NSUnderlyingError=0x170255d20 "The operation couldn’t be completed. (OSStatus error -16803.)", NSLocalizedFailureReason=An unknown error occurred (-16803), NSLocalizedDescription=The operation could not be completed}
回答1:
Try
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in videoDevices)
{
NSLog(device.description);
}
to see if you received message like this:
AVCaptureFigVideoDevice: hex[Back Camera][com.apple.avfoundation.avcapturedevice.built-in_video:0]
I had a problem when I couldn't use camera and it wouldn't appear in device list. Then I noticed that even the standart camera app wouldn't work. So I just reloaded my iPad and it solved my problem. I think that during testing my application I managed to change something important somehow.
回答2:
Check "...more code"
The frontcamera will return an image faster, so if you stop the preview or make other changes to videoConnection directly after captureStillImageAsynchronouslyFromConnection it might work for front camera but not for back camera.
回答3:
My solution :
captureStillImageAsynchronouslyFromConnection
is on capturePhoto
method.
Add :
if(!CMSampleBufferIsValid(imageSampleBuffer))
{
[self capturePhoto];
return;
}
Method seems like that :
- (void) capturePhoto {
...SOME CODE...
[_stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
if(!CMSampleBufferIsValid(imageSampleBuffer))//Check if capture failed
{
[self capturePhoto];
return;
}
...SOME CODE...
}];
}
来源:https://stackoverflow.com/questions/29098747/issue-with-capturestillimageasynchronouslyfromconnection-for-back-camera