Skip to content

Commit 15e6547

Browse files
committed
Add orientation on capture flag for picture and video
2 parents 0e4df8a + 0188995 commit 15e6547

File tree

8 files changed

+236
-185
lines changed

8 files changed

+236
-185
lines changed

LLSimpleCamera.podspec

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "LLSimpleCamera"
3-
s.version = "4.2.0"
3+
s.version = "5.0.0"
44
s.summary = "LLSimpleCamera: A simple customizable camera - video recorder control."
55
s.description = <<-DESC
66
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.
@@ -15,7 +15,7 @@ hides the nitty gritty details from the developer
1515
s.license = { :type => 'APACHE', :file => 'LICENSE' }
1616
s.author = { "Ömer Faruk Gül" => "[email protected]" }
1717
s.platform = :ios,'7.0'
18-
s.source = { :git => "https://github.com/omergul123/LLSimpleCamera.git", :tag => "v4.2.0" }
18+
s.source = { :git => "https://github.com/omergul123/LLSimpleCamera.git", :tag => "v5.0.0" }
1919
s.source_files = 'LLSimpleCamera/*.{h,m}'
2020
s.requires_arc = true
2121
s.framework = 'AVFoundation'
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// LLSimpleCamera+Helper.h
3+
// LLSimpleCameraExample
4+
//
5+
// Created by Ömer Faruk Gül on 20/02/16.
6+
// Copyright © 2016 Ömer Faruk Gül. All rights reserved.
7+
//
8+
9+
#import "LLSimpleCamera.h"
10+
11+
@interface LLSimpleCamera (Helper)
12+
13+
- (CGPoint)convertToPointOfInterestFromViewCoordinates:(CGPoint)viewCoordinates
14+
previewLayer:(AVCaptureVideoPreviewLayer *)previewLayer
15+
ports:(NSArray<AVCaptureInputPort *> *)ports;
16+
17+
- (UIImage *)cropImage:(UIImage *)image usingPreviewLayer:(AVCaptureVideoPreviewLayer *)previewLayer;
18+
19+
@end
+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//
2+
// LLSimpleCamera+Helper.m
3+
// LLSimpleCameraExample
4+
//
5+
// Created by Ömer Faruk Gül on 20/02/16.
6+
// Copyright © 2016 Ömer Faruk Gül. All rights reserved.
7+
//
8+
9+
#import "LLSimpleCamera+Helper.h"
10+
11+
@implementation LLSimpleCamera (Helper)
12+
13+
- (CGPoint)convertToPointOfInterestFromViewCoordinates:(CGPoint)viewCoordinates
14+
previewLayer:(AVCaptureVideoPreviewLayer *)previewLayer
15+
ports:(NSArray<AVCaptureInputPort *> *)ports
16+
{
17+
CGPoint pointOfInterest = CGPointMake(.5f, .5f);
18+
CGSize frameSize = previewLayer.frame.size;
19+
20+
if ( [previewLayer.videoGravity isEqualToString:AVLayerVideoGravityResize] ) {
21+
pointOfInterest = CGPointMake(viewCoordinates.y / frameSize.height, 1.f - (viewCoordinates.x / frameSize.width));
22+
} else {
23+
CGRect cleanAperture;
24+
for (AVCaptureInputPort *port in ports) {
25+
if (port.mediaType == AVMediaTypeVideo) {
26+
cleanAperture = CMVideoFormatDescriptionGetCleanAperture([port formatDescription], YES);
27+
CGSize apertureSize = cleanAperture.size;
28+
CGPoint point = viewCoordinates;
29+
30+
CGFloat apertureRatio = apertureSize.height / apertureSize.width;
31+
CGFloat viewRatio = frameSize.width / frameSize.height;
32+
CGFloat xc = .5f;
33+
CGFloat yc = .5f;
34+
35+
if ( [previewLayer.videoGravity isEqualToString:AVLayerVideoGravityResizeAspect] ) {
36+
if (viewRatio > apertureRatio) {
37+
CGFloat y2 = frameSize.height;
38+
CGFloat x2 = frameSize.height * apertureRatio;
39+
CGFloat x1 = frameSize.width;
40+
CGFloat blackBar = (x1 - x2) / 2;
41+
if (point.x >= blackBar && point.x <= blackBar + x2) {
42+
xc = point.y / y2;
43+
yc = 1.f - ((point.x - blackBar) / x2);
44+
}
45+
} else {
46+
CGFloat y2 = frameSize.width / apertureRatio;
47+
CGFloat y1 = frameSize.height;
48+
CGFloat x2 = frameSize.width;
49+
CGFloat blackBar = (y1 - y2) / 2;
50+
if (point.y >= blackBar && point.y <= blackBar + y2) {
51+
xc = ((point.y - blackBar) / y2);
52+
yc = 1.f - (point.x / x2);
53+
}
54+
}
55+
} else if ([previewLayer.videoGravity isEqualToString:AVLayerVideoGravityResizeAspectFill]) {
56+
if (viewRatio > apertureRatio) {
57+
CGFloat y2 = apertureSize.width * (frameSize.width / apertureSize.height);
58+
xc = (point.y + ((y2 - frameSize.height) / 2.f)) / y2;
59+
yc = (frameSize.width - point.x) / frameSize.width;
60+
} else {
61+
CGFloat x2 = apertureSize.height * (frameSize.height / apertureSize.width);
62+
yc = 1.f - ((point.x + ((x2 - frameSize.width) / 2)) / x2);
63+
xc = point.y / frameSize.height;
64+
}
65+
}
66+
67+
pointOfInterest = CGPointMake(xc, yc);
68+
break;
69+
}
70+
}
71+
}
72+
73+
return pointOfInterest;
74+
}
75+
76+
- (UIImage *)cropImage:(UIImage *)image usingPreviewLayer:(AVCaptureVideoPreviewLayer *)previewLayer
77+
{
78+
CGRect previewBounds = previewLayer.bounds;
79+
CGRect outputRect = [previewLayer metadataOutputRectOfInterestForRect:previewBounds];
80+
81+
CGImageRef takenCGImage = image.CGImage;
82+
size_t width = CGImageGetWidth(takenCGImage);
83+
size_t height = CGImageGetHeight(takenCGImage);
84+
CGRect cropRect = CGRectMake(outputRect.origin.x * width, outputRect.origin.y * height,
85+
outputRect.size.width * width, outputRect.size.height * height);
86+
87+
CGImageRef cropCGImage = CGImageCreateWithImageInRect(takenCGImage, cropRect);
88+
image = [UIImage imageWithCGImage:cropCGImage scale:1 orientation:image.imageOrientation];
89+
CGImageRelease(cropCGImage);
90+
91+
return image;
92+
}
93+
94+
@end

LLSimpleCamera/LLSimpleCamera.h

+14-5
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ typedef enum : NSUInteger {
118118

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

@@ -154,6 +154,15 @@ typedef enum : NSUInteger {
154154
*/
155155
- (void)stop;
156156

157+
158+
/**
159+
* Capture an image.
160+
* @param onCapture a block triggered after the capturing the photo.
161+
* @param exactSeenImage If set YES, then the image is cropped to the exact size as the preview. So you get exactly what you see.
162+
* @param animationBlock you can create your own animation by playing with preview layer.
163+
*/
164+
-(void)capture:(void (^)(LLSimpleCamera *camera, UIImage *image, NSDictionary *metadata, NSError *error))onCapture exactSeenImage:(BOOL)exactSeenImage animationBlock:(void (^)(AVCaptureVideoPreviewLayer *))animationBlock;
165+
157166
/**
158167
* Capture an image.
159168
* @param onCapture a block triggered after the capturing the photo.
@@ -168,14 +177,14 @@ typedef enum : NSUInteger {
168177
-(void)capture:(void (^)(LLSimpleCamera *camera, UIImage *image, NSDictionary *metadata, NSError *error))onCapture;
169178

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

175184
/**
176-
* Stop recording video with a completion block.
185+
* Stop recording video.
177186
*/
178-
- (void)stopRecording:(void (^)(LLSimpleCamera *camera, NSURL *outputFileUrl, NSError *error))completionBlock;
187+
- (void)stopRecording;
179188

180189
/**
181190
* Attaches the LLSimpleCamera to another view controller with a frame. It basically adds the LLSimpleCamera as a

0 commit comments

Comments
 (0)