react-native-camera/RCTCamera.m

90 lines
2.2 KiB
Mathematica
Raw Normal View History

2015-04-01 01:02:57 +00:00
#import "RCTCamera.h"
#import "RCTLog.h"
#import "ViewfinderView.h"
#import <AVFoundation/AVFoundation.h>
@implementation RCTCamera
- (void)setOrientation:(NSInteger)orientation
2015-04-01 01:02:57 +00:00
{
[[(AVCaptureVideoPreviewLayer *)[[self viewfinder] layer] connection] setVideoOrientation:orientation];
}
- (void)setAspect:(NSString *)aspect
{
[(AVCaptureVideoPreviewLayer *)[[self viewfinder] layer] setVideoGravity:aspect];
2015-04-01 01:02:57 +00:00
}
- (id)init
{
if ((self = [super init])) {
_viewfinder = [[ViewfinderView alloc] init];
AVCaptureSession *session = [[AVCaptureSession alloc] init];
[[self viewfinder] setSession:session];
[self addSubview:_viewfinder];
2015-04-01 01:02:57 +00:00
NSError *error = nil;
2015-04-01 01:02:57 +00:00
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *captureDevice = [devices firstObject];
2015-04-01 01:02:57 +00:00
AVCaptureDevicePosition position = AVCaptureDevicePositionBack;
2015-04-01 01:02:57 +00:00
for (AVCaptureDevice *device in devices)
{
if ([device position] == position)
{
captureDevice = device;
break;
}
}
2015-04-01 01:02:57 +00:00
AVCaptureDeviceInput *captureDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
2015-04-01 01:02:57 +00:00
if (error)
{
NSLog(@"%@", error);
}
2015-04-01 01:02:57 +00:00
if ([session canAddInput:captureDeviceInput])
{
[session addInput:captureDeviceInput];
[[(AVCaptureVideoPreviewLayer *)[[self viewfinder] layer] connection] setVideoOrientation:AVCaptureVideoOrientationPortrait];
2015-04-01 01:02:57 +00:00
[(AVCaptureVideoPreviewLayer *)[[self viewfinder] layer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
}
2015-04-01 01:02:57 +00:00
[session startRunning];
}
return self;
}
- (NSArray *)reactSubviews
{
NSArray *subviews = @[_viewfinder];
return subviews;
}
- (void)layoutSubviews
{
[super layoutSubviews];
_viewfinder.frame = self.bounds;
}
- (void)insertReactSubview:(UIView *)view atIndex:(NSInteger)atIndex
{
RCTLogError(@"Camera does not support subviews");
return;
}
- (void)removeReactSubview:(UIView *)subview
{
RCTLogError(@"Camera does not support subviews");
return;
}
@end