Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions LLSimpleCamera.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "LLSimpleCamera"
s.version = "2.1.1"
s.version = "2.2.0"
s.summary = "LLSimpleCamera: A simple customizable camera control."
s.description = <<-DESC
LLSimpleCamera is a library for creating a customized camera screens similar to snapchat's. You don't have to present the camera in a new view controller.
Expand All @@ -13,9 +13,9 @@ hides the nitty gritty details from the developer

s.homepage = "https://github.com/omergul123/LLSimpleCamera"
s.license = { :type => 'APACHE', :file => 'LICENSE' }
s.author = { "Ömer Faruk Gül" => "omer.gul@louvredigital.com" }
s.author = { "Ömer Faruk Gül" => "omergul123@gmail.com" }
s.platform = :ios,'7.0'
s.source = { :git => "https://github.com/omergul123/LLSimpleCamera.git", :tag => "v2.1.1" }
s.source = { :git => "https://github.com/omergul123/LLSimpleCamera.git", :tag => "v2.2.0" }
s.source_files = 'LLSimpleCamera/*.{h,m}'
s.requires_arc = true
s.framework = 'AVFoundation'
Expand Down
31 changes: 24 additions & 7 deletions LLSimpleCamera/LLSimpleCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ typedef enum : NSUInteger {
CameraQualityPhoto
} CameraQuality;

extern NSString *const LLSimpleCameraErrorDomain;
typedef enum : NSUInteger {
LLSimpleCameraErrorCodePermission = 10,
LLSimpleCameraErrorCodeSession = 11
} LLSimpleCameraErrorCode;

@interface LLSimpleCamera : UIViewController

/**
Expand All @@ -43,12 +49,12 @@ typedef enum : NSUInteger {
/**
* Camera flash mode.
*/
@property (nonatomic) CameraFlash cameraFlash;
@property (nonatomic, readonly) CameraFlash flash;

/**
* Position of the camera.
*/
@property (nonatomic) CameraPosition cameraPosition;
@property (nonatomic) CameraPosition position;

/**
* Fixess the orientation after the image is captured is set to Yes.
Expand All @@ -61,6 +67,12 @@ typedef enum : NSUInteger {
*/
@property (nonatomic) BOOL tapToFocus;

/**
* Set YES if you your view controller does not allow autorotation,
* however you want to take the device rotation into account no matter what. Disabled by default.
*/
@property (nonatomic) BOOL useDeviceOrientation;

/**
* Returns an instance of LLSimpleCamera with the given quality.
* @param quality The quality of the camera.
Expand All @@ -86,19 +98,24 @@ typedef enum : NSUInteger {
- (void)attachToViewController:(UIViewController *)vc withFrame:(CGRect)frame;

/**
Changes the posiition of the camera (either back or front) and returns the final position.
* Changes the posiition of the camera (either back or front) and returns the final position.
*/
- (CameraPosition)togglePosition;

/**
Checks if flash is avilable for the currently active device.
* Update the flash mode of the camera. Returns true if it is successful. Otherwise false.
*/
- (BOOL)updateFlashMode:(CameraFlash)cameraFlash;

/**
* Checks if flash is avilable for the currently active device.
*/
- (BOOL)isFlashAvailable;

/**
Alter the layer and the animation displayed when the user taps on screen.
@param layer Layer to be displayed
@param animation to be applied after the layer is shown
* Alter the layer and the animation displayed when the user taps on screen.
* @param layer Layer to be displayed
* @param animation to be applied after the layer is shown
*/
- (void)alterFocusBox:(CALayer *)layer animation:(CAAnimation *)animation;

Expand Down
152 changes: 105 additions & 47 deletions LLSimpleCamera/LLSimpleCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ @interface LLSimpleCamera ()
@property (strong, nonatomic) CAAnimation *focusBoxAnimation;
@end

NSString *const LLSimpleCameraErrorDomain = @"LLSimpleCameraErrorDomain";

@implementation LLSimpleCamera

- (instancetype)initWithQuality:(CameraQuality)quality andPosition:(CameraPosition)position {
Expand All @@ -32,6 +34,7 @@ - (instancetype)initWithQuality:(CameraQuality)quality andPosition:(CameraPositi
self.cameraPosition = position;
self.fixOrientationAfterCapture = NO;
self.tapToFocus = YES;
self.useDeviceOrientation = NO;
}

return self;
Expand All @@ -40,7 +43,7 @@ - (instancetype)initWithQuality:(CameraQuality)quality andPosition:(CameraPositi
- (void)viewDidLoad {
[super viewDidLoad];

_cameraFlash = CameraFlashOff;
_flash = CameraFlashOff;

self.view.backgroundColor = [UIColor clearColor];
self.view.autoresizingMask = UIViewAutoresizingNone;
Expand Down Expand Up @@ -113,9 +116,29 @@ - (void) previewTapped: (UIGestureRecognizer *) gestureRecognizer
#pragma mark Camera Actions

- (void)start {

// in iOS7 & iOS8 we have check if we have permission t camera
if ([AVCaptureDevice respondsToSelector:@selector(requestAccessForMediaType: completionHandler:)]) {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[self initialize];
});
} else {
NSError *error = [NSError errorWithDomain:LLSimpleCameraErrorDomain
code:LLSimpleCameraErrorCodePermission
userInfo:nil];
dispatch_async(dispatch_get_main_queue(), ^{
self.onError(self, error);
});
}
}];
} else {
[self initialize];
}
}

- (void)initialize {
if(!_session) {

self.session = [[AVCaptureSession alloc] init];

NSString *sessionPreset = nil;
Expand Down Expand Up @@ -149,7 +172,7 @@ - (void)start {
self.captureVideoPreviewLayer = captureVideoPreviewLayer;

AVCaptureDevicePosition devicePosition;
switch (self.cameraPosition) {
switch (self.position) {
case CameraPositionBack:
devicePosition = AVCaptureDevicePositionBack;
break;
Expand Down Expand Up @@ -200,12 +223,20 @@ - (void)stop {

-(void)capture:(void (^)(LLSimpleCamera *camera, UIImage *image, NSDictionary *metadata, NSError *error))onCapture exactSeenImage:(BOOL)exactSeenImage {

if(!self.session) {
NSError *error = [NSError errorWithDomain:LLSimpleCameraErrorDomain
code:LLSimpleCameraErrorCodeSession
userInfo:nil];
onCapture(self, nil, nil, error);
return;
}

// get connection and set orientation
AVCaptureConnection *videoConnection = [self captureConnection];
videoConnection.videoOrientation = [self orientationForConnection];

[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
//Stop capturing data to freeze the screen to indicate the pictrue has been taken
[self.captureVideoPreviewLayer.connection setEnabled:NO];

Expand Down Expand Up @@ -282,8 +313,18 @@ - (AVCaptureConnection *)captureConnection {
- (void)setCaptureDevice:(AVCaptureDevice *)captureDevice {
_captureDevice = captureDevice;

// reset flash
self.cameraFlash = CameraFlashOff;
if(captureDevice.flashMode == AVCaptureFlashModeAuto) {
_flash = CameraFlashAuto;
}
else if(captureDevice.flashMode == AVCaptureFlashModeOn) {
_flash = CameraFlashOn;
}
else if(captureDevice.flashMode == AVCaptureFlashModeOff) {
_flash = CameraFlashOff;
}
else {
_flash = CameraFlashOff;
}

// trigger block
if(self.onDeviceChange) {
Expand All @@ -295,11 +336,12 @@ - (BOOL)isFlashAvailable {
return self.captureDevice.isFlashAvailable;
}


-(void)setCameraFlash:(CameraFlash)cameraFlash {
- (BOOL)updateFlashMode:(CameraFlash)cameraFlash {
if(!self.session)
return NO;

AVCaptureFlashMode flashMode;

if(cameraFlash == CameraFlashOn) {
flashMode = AVCaptureFlashModeOn;
}
Expand All @@ -310,49 +352,44 @@ -(void)setCameraFlash:(CameraFlash)cameraFlash {
flashMode = AVCaptureFlashModeOff;
}

BOOL done = [self setFlashMode:flashMode];

if(done) {
_cameraFlash = cameraFlash;
}
else {
_cameraFlash = CameraFlashOff;
}
}

- (BOOL) setFlashMode:(AVCaptureFlashMode)flashMode
{
if([_captureDevice isFlashModeSupported:flashMode]) {

if(_captureDevice.flashMode == flashMode) {
return YES;
}

if([_captureDevice lockForConfiguration:nil]) {
NSError *error;
if([_captureDevice lockForConfiguration:&error]) {
_captureDevice.flashMode = flashMode;
[_captureDevice unlockForConfiguration];

_flash = cameraFlash;
return YES;
}
else {
self.onError(self, error);
return NO;
}
}
else {
return NO;
}

return NO;
}

- (CameraPosition)togglePosition {
if(self.cameraPosition == CameraPositionBack) {
if(!self.session) {
return self.position;
}

if(self.position == CameraPositionBack) {
self.cameraPosition = CameraPositionFront;
}
else {
self.cameraPosition = CameraPositionBack;
}

return self.cameraPosition;
return self.position;
}

- (void)setCameraPosition:(CameraPosition)cameraPosition
{
if(_cameraPosition == cameraPosition) {
if(_position == cameraPosition || !self.session) {
return;
}

Expand Down Expand Up @@ -387,7 +424,7 @@ - (void)setCameraPosition:(CameraPosition)cameraPosition
return;
}

_cameraPosition = cameraPosition;
_position = cameraPosition;

[self.session addInput:newVideoInput];
[self.session commitConfiguration];
Expand Down Expand Up @@ -537,20 +574,41 @@ - (void)viewWillLayoutSubviews {
- (AVCaptureVideoOrientation)orientationForConnection
{
AVCaptureVideoOrientation videoOrientation = AVCaptureVideoOrientationPortrait;
switch (self.interfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
break;
case UIInterfaceOrientationLandscapeRight:
videoOrientation = AVCaptureVideoOrientationLandscapeRight;
break;
case UIInterfaceOrientationPortraitUpsideDown:
videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
break;
default:
videoOrientation = AVCaptureVideoOrientationPortrait;
break;

if(self.useDeviceOrientation) {
switch ([UIDevice currentDevice].orientation) {
case UIDeviceOrientationLandscapeLeft:
// yes we to the right, this is not bug!
videoOrientation = AVCaptureVideoOrientationLandscapeRight;
break;
case UIDeviceOrientationLandscapeRight:
videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
break;
case UIDeviceOrientationPortraitUpsideDown:
videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
break;
default:
videoOrientation = AVCaptureVideoOrientationPortrait;
break;
}
}
else {
switch (self.interfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
break;
case UIInterfaceOrientationLandscapeRight:
videoOrientation = AVCaptureVideoOrientationLandscapeRight;
break;
case UIInterfaceOrientationPortraitUpsideDown:
videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
break;
default:
videoOrientation = AVCaptureVideoOrientationPortrait;
break;
}
}

return videoOrientation;
}

Expand Down
Loading