Skip to content

Commit

Permalink
Contains fixes for the viewfinder.
Browse files Browse the repository at this point in the history
  • Loading branch information
lwansbrough committed Apr 17, 2015
1 parent 488e532 commit 6d7d360
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 52 deletions.
2 changes: 0 additions & 2 deletions RCTCamera.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import "ViewfinderView.h"

@class RCTCameraManager;

@interface RCTCamera : UIView

@property (nonatomic) ViewfinderView *viewfinder;
@property (nonatomic) RCTCameraManager *manager;

- (id)initWithManager:(RCTCameraManager*)manager;
Expand Down
15 changes: 4 additions & 11 deletions RCTCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
#import "RCTCameraManager.h"
#import "RCTLog.h"
#import "RCTUtils.h"
#import "ViewfinderView.h"

#import <AVFoundation/AVFoundation.h>

@implementation RCTCamera

- (void)setAspect:(NSString *)aspect
{
[(AVCaptureVideoPreviewLayer *)[_viewfinder layer] setVideoGravity:aspect];
[self.manager changeAspect:aspect];
}

- (void)setType:(NSInteger)camera
Expand All @@ -31,24 +30,18 @@ - (void)setOrientation:(NSInteger)orientation

- (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
Expand Down
3 changes: 2 additions & 1 deletion RCTCameraManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 11 additions & 5 deletions RCTCameraManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ @implementation RCTCameraManager

- (UIView *)view
{
self.currentCamera = [[RCTCamera alloc] initWithManager:self];
return self.currentCamera;
return [[RCTCamera alloc] initWithManager:self];
}

RCT_EXPORT_VIEW_PROPERTY(aspect, NSString);
Expand Down Expand Up @@ -45,9 +44,13 @@ - (NSDictionary *)constantsToExport
- (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, ^{
Expand Down Expand Up @@ -136,16 +139,19 @@ - (id)init {
[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)
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 0 additions & 9 deletions ViewfinderView.h

This file was deleted.

21 changes: 0 additions & 21 deletions ViewfinderView.m

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]> (http://lwansbrough.com)",
Expand Down

0 comments on commit 6d7d360

Please sign in to comment.