I have a preview layer that is pulling from the camera and working as it should. I would like to be able to take a picture when I press a button. I have inited the AVCaptureStil
-(void)captureImage:(NSString *)string successCallback:(void (^)(id))successCallback errorCallback:(void (^)(NSString *))errorCallback{
__block UIImage *image;
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in stillImageOutput.connections)
{
for (AVCaptureInputPort *port in [connection inputPorts])
{
if ([[port mediaType] isEqual:AVMediaTypeVideo] )
{
videoConnection = connection;
break;
}
}
if (videoConnection)
{
break;
}
}
//NSLog(@"about to request a capture from: %@", stillImageOutput);
[videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
if (exifAttachments)
{
// Do something with the attachments.
//NSLog(@"attachements: %@", exifAttachments);
} else {
//NSLog(@"no attachments");
}
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
image = [[UIImage alloc] initWithData:imageData];
successCallback(image);
//UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}];
NSError *error;
if (error) {
errorCallback(@"error");
}else{
}
}