Merge pull request #20 from rt2zz/master

Add capturePictureToDisk method - transitional
This commit is contained in:
Loch Wansbrough 2015-04-30 22:54:08 -07:00
commit 1a9eebc5d6
3 changed files with 39 additions and 0 deletions

View File

@ -73,7 +73,12 @@ var Camera = React.createClass({
takePicture: function(cb) {
NativeModules.CameraManager.takePicture(cb);
},
capturePictureToDisk: function(cb) {
NativeModules.CameraManager.capturePictureToDisk(cb)
}
});
var RCTCamera = createReactIOSNativeComponentClass({

View File

@ -19,5 +19,6 @@
- (void)changeOrientation:(NSInteger)orientation;
- (AVCaptureDevice *)deviceWithMediaType:(NSString *)mediaType preferringPosition:(AVCaptureDevicePosition)position;
- (void)takePicture:(RCTResponseSenderBlock)callback;
- (void)capturePictureToDisk:(RCTResponseSenderBlock)callback;
@end

View File

@ -184,6 +184,39 @@ RCT_EXPORT_METHOD(takePicture:(RCTResponseSenderBlock)callback) {
}];
}
RCT_EXPORT_METHOD(capturePictureToDisk:(RCTResponseSenderBlock)callback) {
[[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:self.previewLayer.connection.videoOrientation];
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (imageDataSampleBuffer)
{
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [UIImage imageWithData:imageData];
UIImage *rotatedImage = [image resizedImage:CGSizeMake(image.size.width, image.size.height) interpolationQuality:kCGInterpolationDefault];
NSString *uuid = [[NSUUID UUID] UUIDString];
NSString *fullPath = [self saveImage:rotatedImage withName:uuid];
callback(@[[NSNull null], fullPath]);
}
else {
callback(@[RCTMakeError(error.description, nil, nil)]);
}
}];
}
- (NSString *)saveImage:(UIImage *)image withName:(NSString *)name {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths firstObject];
NSData *data = UIImageJPEGRepresentation(image, 1.0);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
NSLog(@"writing path %@", fullPath);
[fileManager createFileAtPath:fullPath contents:data attributes:nil];
return fullPath;
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
NSArray *barcodeTypes = @[