Created
November 9, 2011 01:13
-
-
Save thiagoperes/1349984 to your computer and use it in GitHub Desktop.
exempo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (AVCaptureConnection *)connectionWithMediaType:(NSString *)mediaType fromConnections:(NSArray *)connections | |
{ | |
for ( AVCaptureConnection *connection in connections ) { | |
for ( AVCaptureInputPort *port in [connection inputPorts] ) { | |
if ( [[port mediaType] isEqual:mediaType] ) { | |
return connection; | |
} | |
} | |
} | |
return nil; | |
} | |
- (void) captureStillImage | |
{ | |
AVCaptureConnection *stillImageConnection = [AVCamUtilities connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self stillImageOutput] connections]]; | |
if ([stillImageConnection isVideoOrientationSupported]) | |
[stillImageConnection setVideoOrientation:orientation]; | |
[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:stillImageConnection | |
completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { | |
ALAssetsLibraryWriteImageCompletionBlock completionBlock = ^(NSURL *assetURL, NSError *error) { | |
if (error) { | |
if ([[self delegate] respondsToSelector:@selector(captureManager:didFailWithError:)]) { | |
[[self delegate] captureManager:self didFailWithError:error]; | |
} | |
} | |
}; | |
if (imageDataSampleBuffer != NULL) { | |
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; | |
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; | |
UIImage *image = [[UIImage alloc] initWithData:imageData]; | |
[library writeImageToSavedPhotosAlbum:[image CGImage] | |
orientation:(ALAssetOrientation)[image imageOrientation] | |
completionBlock:completionBlock]; | |
[image release]; | |
[library release]; | |
} | |
else | |
completionBlock(nil, error); | |
if ([[self delegate] respondsToSelector:@selector(captureManagerStillImageCaptured:)]) { | |
[[self delegate] captureManagerStillImageCaptured:self]; | |
} | |
}]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment