diff --git a/RCTCamera.h b/RCTCamera.h index fe18d8e..83daf61 100644 --- a/RCTCamera.h +++ b/RCTCamera.h @@ -1,12 +1,10 @@ #import #import -#import "ViewfinderView.h" @class RCTCameraManager; @interface RCTCamera : UIView -@property (nonatomic) ViewfinderView *viewfinder; @property (nonatomic) RCTCameraManager *manager; - (id)initWithManager:(RCTCameraManager*)manager; diff --git a/RCTCamera.m b/RCTCamera.m index 1f324d0..ca446db 100644 --- a/RCTCamera.m +++ b/RCTCamera.m @@ -3,7 +3,6 @@ #import "RCTCameraManager.h" #import "RCTLog.h" #import "RCTUtils.h" -#import "ViewfinderView.h" #import @@ -11,7 +10,7 @@ - (void)setAspect:(NSString *)aspect { - [(AVCaptureVideoPreviewLayer *)[_viewfinder layer] setVideoGravity:aspect]; + [self.manager changeAspect:aspect]; } - (void)setType:(NSInteger)camera @@ -31,24 +30,18 @@ - (id)initWithManager:(RCTCameraManager*)manager { + if ((self = [super init])) { self.manager = manager; - self.viewfinder = [[ViewfinderView alloc] init]; - self.viewfinder.session = self.manager.session; } return self; } -- (NSArray *)reactSubviews -{ - NSArray *subviews = @[self.viewfinder]; - return subviews; -} - - (void)layoutSubviews { [super layoutSubviews]; - [self.viewfinder setFrame:self.bounds]; + self.manager.previewLayer.frame = self.bounds; + [self.layer insertSublayer:self.manager.previewLayer atIndex:0]; } - (void)insertReactSubview:(UIView *)view atIndex:(NSInteger)atIndex diff --git a/RCTCameraManager.h b/RCTCameraManager.h index e1f4dd5..955692a 100644 --- a/RCTCameraManager.h +++ b/RCTCameraManager.h @@ -11,8 +11,9 @@ @property (nonatomic) AVCaptureStillImageOutput *stillImageOutput; @property (nonatomic) id runtimeErrorHandlingObserver; @property (nonatomic) NSInteger presetCamera; -@property (nonatomic) RCTCamera *currentCamera; +@property (nonatomic) AVCaptureVideoPreviewLayer *previewLayer; +- (void)changeAspect:(NSString *)aspect; - (void)changeCamera:(NSInteger)camera; - (void)changeOrientation:(NSInteger)orientation; - (AVCaptureDevice *)deviceWithMediaType:(NSString *)mediaType preferringPosition:(AVCaptureDevicePosition)position; diff --git a/RCTCameraManager.m b/RCTCameraManager.m index 1375cc2..1249e52 100644 --- a/RCTCameraManager.m +++ b/RCTCameraManager.m @@ -13,8 +13,7 @@ RCT_EXPORT_MODULE(); - (UIView *)view { - self.currentCamera = [[RCTCamera alloc] initWithManager:self]; - return self.currentCamera; + return [[RCTCamera alloc] initWithManager:self]; } RCT_EXPORT_VIEW_PROPERTY(aspect, NSString); @@ -45,9 +44,13 @@ RCT_EXPORT_VIEW_PROPERTY(orientation, NSInteger); - (id)init { if ((self = [super init])) { + self.session = [AVCaptureSession new]; self.session.sessionPreset = AVCaptureSessionPresetHigh; + self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.session]; + self.previewLayer.needsDisplayOnBoundsChange = YES; + self.sessionQueue = dispatch_queue_create("cameraManagerQueue", DISPATCH_QUEUE_SERIAL); dispatch_async(self.sessionQueue, ^{ @@ -136,16 +139,19 @@ RCT_EXPORT_METHOD(changeCamera:(NSInteger)camera) { [self.session addInput:self.captureDeviceInput]; } - [self.session commitConfiguration]; } +RCT_EXPORT_METHOD(changeAspect:(NSString *)aspect) { + self.previewLayer.videoGravity = aspect; +} + RCT_EXPORT_METHOD(changeOrientation:(NSInteger)orientation) { - ((AVCaptureVideoPreviewLayer *)self.currentCamera.viewfinder.layer).connection.videoOrientation = orientation; + self.previewLayer.connection.videoOrientation = orientation; } RCT_EXPORT_METHOD(takePicture:(RCTResponseSenderBlock)callback) { - [[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:((AVCaptureVideoPreviewLayer *)self.currentCamera.viewfinder.layer).connection.videoOrientation]; + [[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:self.previewLayer.connection.videoOrientation]; [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { if (imageDataSampleBuffer) diff --git a/README.md b/README.md index ac9438c..d39f372 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,10 @@ # react-native-camera -A camera viewport for React Native. This module is currently in the very early stages of development. **Module is blowing up right now with recent changes from the core React Native team. Change is good! But it's breaking this module. Be warned.** +A camera viewport for React Native. This module is currently in the very early stages of development. ## Known Issues Below is a list of known issues. Pull requests are welcome for any of these issues! -- Viewfinder *does not work*. This is a temporary issue that I'm resolving, but right now it will appear as the module is not working. The camera does in fact work though. - [Camera module may cause app to crash in simulator](https://github.com/lwansbrough/react-native-camera/issues/8) ## Getting started diff --git a/ViewfinderView.h b/ViewfinderView.h deleted file mode 100644 index c7415e9..0000000 --- a/ViewfinderView.h +++ /dev/null @@ -1,9 +0,0 @@ -#import - -@class AVCaptureSession; - -@interface ViewfinderView : UIView - -@property (nonatomic) AVCaptureSession *session; - -@end diff --git a/ViewfinderView.m b/ViewfinderView.m deleted file mode 100644 index fd6e14a..0000000 --- a/ViewfinderView.m +++ /dev/null @@ -1,21 +0,0 @@ -#import "ViewfinderView.h" -#import - -@implementation ViewfinderView - -+ (Class)layerClass -{ - return [AVCaptureVideoPreviewLayer class]; -} - -- (AVCaptureSession *)session -{ - return [(AVCaptureVideoPreviewLayer *)[self layer] session]; -} - -- (void)setSession:(AVCaptureSession *)session -{ - [(AVCaptureVideoPreviewLayer *)[self layer] setSession:session]; -} - -@end \ No newline at end of file diff --git a/package.json b/package.json index 7d07275..9bf44fb 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "type" : "git", "url" : "https://github.com/lwansbrough/react-native-camera.git" }, - "version": "0.0.8", + "version": "0.0.9", "description": "A Camera element for React Native", "main": "Camera.ios.js", "author": "Lochlan Wansbrough (http://lwansbrough.com)",