Skip to content

Commit

Permalink
Merge master branch from omergul123/LLSimpleCamera
Browse files Browse the repository at this point in the history
  • Loading branch information
leoneparise committed May 18, 2016
2 parents 0e4df8a + 0188995 commit 8e5fc4a
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 185 deletions.
4 changes: 2 additions & 2 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 = "4.2.0"
s.version = "5.0.0"
s.summary = "LLSimpleCamera: A simple customizable camera - video recorder 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. You can capture images or record videos very easily.
Expand All @@ -15,7 +15,7 @@ hides the nitty gritty details from the developer
s.license = { :type => 'APACHE', :file => 'LICENSE' }
s.author = { "Ömer Faruk Gül" => "omergul123@gmail.com" }
s.platform = :ios,'7.0'
s.source = { :git => "https://github.com/omergul123/LLSimpleCamera.git", :tag => "v4.2.0" }
s.source = { :git => "https://github.com/omergul123/LLSimpleCamera.git", :tag => "v5.0.0" }
s.source_files = 'LLSimpleCamera/*.{h,m}'
s.requires_arc = true
s.framework = 'AVFoundation'
Expand Down
19 changes: 19 additions & 0 deletions LLSimpleCamera/LLSimpleCamera+Helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// LLSimpleCamera+Helper.h
// LLSimpleCameraExample
//
// Created by Ömer Faruk Gül on 20/02/16.
// Copyright © 2016 Ömer Faruk Gül. All rights reserved.
//

#import "LLSimpleCamera.h"

@interface LLSimpleCamera (Helper)

- (CGPoint)convertToPointOfInterestFromViewCoordinates:(CGPoint)viewCoordinates
previewLayer:(AVCaptureVideoPreviewLayer *)previewLayer
ports:(NSArray<AVCaptureInputPort *> *)ports;

- (UIImage *)cropImage:(UIImage *)image usingPreviewLayer:(AVCaptureVideoPreviewLayer *)previewLayer;

@end
94 changes: 94 additions & 0 deletions LLSimpleCamera/LLSimpleCamera+Helper.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//
// LLSimpleCamera+Helper.m
// LLSimpleCameraExample
//
// Created by Ömer Faruk Gül on 20/02/16.
// Copyright © 2016 Ömer Faruk Gül. All rights reserved.
//

#import "LLSimpleCamera+Helper.h"

@implementation LLSimpleCamera (Helper)

- (CGPoint)convertToPointOfInterestFromViewCoordinates:(CGPoint)viewCoordinates
previewLayer:(AVCaptureVideoPreviewLayer *)previewLayer
ports:(NSArray<AVCaptureInputPort *> *)ports
{
CGPoint pointOfInterest = CGPointMake(.5f, .5f);
CGSize frameSize = previewLayer.frame.size;

if ( [previewLayer.videoGravity isEqualToString:AVLayerVideoGravityResize] ) {
pointOfInterest = CGPointMake(viewCoordinates.y / frameSize.height, 1.f - (viewCoordinates.x / frameSize.width));
} else {
CGRect cleanAperture;
for (AVCaptureInputPort *port in ports) {
if (port.mediaType == AVMediaTypeVideo) {
cleanAperture = CMVideoFormatDescriptionGetCleanAperture([port formatDescription], YES);
CGSize apertureSize = cleanAperture.size;
CGPoint point = viewCoordinates;

CGFloat apertureRatio = apertureSize.height / apertureSize.width;
CGFloat viewRatio = frameSize.width / frameSize.height;
CGFloat xc = .5f;
CGFloat yc = .5f;

if ( [previewLayer.videoGravity isEqualToString:AVLayerVideoGravityResizeAspect] ) {
if (viewRatio > apertureRatio) {
CGFloat y2 = frameSize.height;
CGFloat x2 = frameSize.height * apertureRatio;
CGFloat x1 = frameSize.width;
CGFloat blackBar = (x1 - x2) / 2;
if (point.x >= blackBar && point.x <= blackBar + x2) {
xc = point.y / y2;
yc = 1.f - ((point.x - blackBar) / x2);
}
} else {
CGFloat y2 = frameSize.width / apertureRatio;
CGFloat y1 = frameSize.height;
CGFloat x2 = frameSize.width;
CGFloat blackBar = (y1 - y2) / 2;
if (point.y >= blackBar && point.y <= blackBar + y2) {
xc = ((point.y - blackBar) / y2);
yc = 1.f - (point.x / x2);
}
}
} else if ([previewLayer.videoGravity isEqualToString:AVLayerVideoGravityResizeAspectFill]) {
if (viewRatio > apertureRatio) {
CGFloat y2 = apertureSize.width * (frameSize.width / apertureSize.height);
xc = (point.y + ((y2 - frameSize.height) / 2.f)) / y2;
yc = (frameSize.width - point.x) / frameSize.width;
} else {
CGFloat x2 = apertureSize.height * (frameSize.height / apertureSize.width);
yc = 1.f - ((point.x + ((x2 - frameSize.width) / 2)) / x2);
xc = point.y / frameSize.height;
}
}

pointOfInterest = CGPointMake(xc, yc);
break;
}
}
}

return pointOfInterest;
}

- (UIImage *)cropImage:(UIImage *)image usingPreviewLayer:(AVCaptureVideoPreviewLayer *)previewLayer
{
CGRect previewBounds = previewLayer.bounds;
CGRect outputRect = [previewLayer metadataOutputRectOfInterestForRect:previewBounds];

CGImageRef takenCGImage = image.CGImage;
size_t width = CGImageGetWidth(takenCGImage);
size_t height = CGImageGetHeight(takenCGImage);
CGRect cropRect = CGRectMake(outputRect.origin.x * width, outputRect.origin.y * height,
outputRect.size.width * width, outputRect.size.height * height);

CGImageRef cropCGImage = CGImageCreateWithImageInRect(takenCGImage, cropRect);
image = [UIImage imageWithCGImage:cropCGImage scale:1 orientation:image.imageOrientation];
CGImageRelease(cropCGImage);

return image;
}

@end
19 changes: 14 additions & 5 deletions LLSimpleCamera/LLSimpleCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ typedef enum : NSUInteger {

/**
* Set YES if you your view controller does not allow autorotation,
* but you want to use the devide orientation on capture. Enabled by default.
* but you want to use the devide orientation on capture. Disabled by default.
*/
@property (nonatomic) BOOL useDeviceOrientationOnCapture;

Expand Down Expand Up @@ -154,6 +154,15 @@ typedef enum : NSUInteger {
*/
- (void)stop;


/**
* Capture an image.
* @param onCapture a block triggered after the capturing the photo.
* @param exactSeenImage If set YES, then the image is cropped to the exact size as the preview. So you get exactly what you see.
* @param animationBlock you can create your own animation by playing with preview layer.
*/
-(void)capture:(void (^)(LLSimpleCamera *camera, UIImage *image, NSDictionary *metadata, NSError *error))onCapture exactSeenImage:(BOOL)exactSeenImage animationBlock:(void (^)(AVCaptureVideoPreviewLayer *))animationBlock;

/**
* Capture an image.
* @param onCapture a block triggered after the capturing the photo.
Expand All @@ -168,14 +177,14 @@ typedef enum : NSUInteger {
-(void)capture:(void (^)(LLSimpleCamera *camera, UIImage *image, NSDictionary *metadata, NSError *error))onCapture;

/*
* Start recording a video. Video is saved to the given url.
* Start recording a video with a completion block. Video is saved to the given url.
*/
- (void)startRecordingWithOutputUrl:(NSURL *)url;
- (void)startRecordingWithOutputUrl:(NSURL *)url didRecord:(void (^)(LLSimpleCamera *camera, NSURL *outputFileUrl, NSError *error))completionBlock;

/**
* Stop recording video with a completion block.
* Stop recording video.
*/
- (void)stopRecording:(void (^)(LLSimpleCamera *camera, NSURL *outputFileUrl, NSError *error))completionBlock;
- (void)stopRecording;

/**
* Attaches the LLSimpleCamera to another view controller with a frame. It basically adds the LLSimpleCamera as a
Expand Down
Loading

0 comments on commit 8e5fc4a

Please sign in to comment.