Skip to content
Open
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
36 changes: 36 additions & 0 deletions LLSimpleCamera/DBMotionManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// DBMotionManager.h
// DBCamera
//
// Created by iBo on 30/07/14.
// Copyright (c) 2014 PSSD - Daniele Bogo. All rights reserved.
//

#import <UIKit/UIKit.h>

/**
* DBMotionManagerRotationHandler handles the current orientation
*/
typedef void (^DBMotionManagerRotationHandler)(UIDeviceOrientation);

/**
* DBMotionManager detect the orientation using CoreMotion
*/
@interface DBMotionManager : NSObject
/**
* The DBMotionManagerRotationHandler property
*/
@property (nonatomic, copy) DBMotionManagerRotationHandler motionRotationHandler;

/**
* The constructor method of
*
* @return the DBMotionManager instancetype
*/
+ (instancetype) sharedManager;

/**
* Start to detect the rotation
*/
- (void) startMotionHandler;
@end
110 changes: 110 additions & 0 deletions LLSimpleCamera/DBMotionManager.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//
// DBMotionManager.m
// DBCamera
//
// Created by iBo on 30/07/14.
// Copyright (c) 2014 PSSD - Daniele Bogo. All rights reserved.
//

#import "DBMotionManager.h"

#import <CoreMotion/CoreMotion.h>

@interface DBMotionManager () {
CMAccelerometerHandler _motionHandler;
}

@property (nonatomic, strong) CMMotionManager *motionManager;
@property (nonatomic, assign) UIDeviceOrientation lastOrientation;
@end

@implementation DBMotionManager

- (CMMotionManager *) motionManager
{
if ( !_motionManager ) {
_motionManager = [[CMMotionManager alloc] init];
}

return _motionManager;
}

+ (instancetype) sharedManager
{
static DBMotionManager *sharedManager = nil;
static dispatch_once_t predicate = 0;

dispatch_once(&predicate, ^{
sharedManager = [[DBMotionManager alloc] init];
});

return sharedManager;
}

- (id) init
{
self = [super init];

if ( self ) {
if ( [self.motionManager isAccelerometerAvailable] ) {
[self.motionManager setAccelerometerUpdateInterval:.2f];
} else {
[self deviceOrientationDidChangeTo:UIDeviceOrientationLandscapeRight];
}
}

return self;
}

- (void) startMotionHandler
{
__weak typeof(self) weakSelf = self;
_motionHandler = ^ (CMAccelerometerData *accelerometerData, NSError *error) {
typeof(self) selfBlock = weakSelf;

CGFloat xx = accelerometerData.acceleration.x;
CGFloat yy = -accelerometerData.acceleration.y;
CGFloat zz = accelerometerData.acceleration.z;

CGFloat device_angle = M_PI / 2.0f - atan2(yy, xx);
UIDeviceOrientation orientation = UIDeviceOrientationUnknown;

if (device_angle > M_PI)
device_angle -= 2 * M_PI;

if ((zz < -.60f) || (zz > .60f)) {
if ( UIDeviceOrientationIsLandscape(selfBlock.lastOrientation) )
orientation = selfBlock.lastOrientation;
else
orientation = UIDeviceOrientationUnknown;
} else {
if ( (device_angle > -M_PI_4) && (device_angle < M_PI_4) )
orientation = UIDeviceOrientationPortrait;
else if ((device_angle < -M_PI_4) && (device_angle > -3 * M_PI_4))
orientation = UIDeviceOrientationLandscapeLeft;
else if ((device_angle > M_PI_4) && (device_angle < 3 * M_PI_4))
orientation = UIDeviceOrientationLandscapeRight;
else
orientation = UIDeviceOrientationPortraitUpsideDown;
}

if (orientation != selfBlock.lastOrientation) {
dispatch_async(dispatch_get_main_queue(), ^{
[selfBlock deviceOrientationDidChangeTo:orientation];
});
}
};

[self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:_motionHandler];
}

- (void) deviceOrientationDidChangeTo:(UIDeviceOrientation)orientation
{
[self setLastOrientation:orientation];

if (self.motionRotationHandler) {
self.motionRotationHandler(self.lastOrientation);
}
}

@end
25 changes: 24 additions & 1 deletion LLSimpleCamera/LLSimpleCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import <ImageIO/CGImageProperties.h>
#import "UIImage+FixOrientation.h"
#import "LLSimpleCamera+Helper.h"
#import "DBMotionManager.h"

@interface LLSimpleCamera () <AVCaptureFileOutputRecordingDelegate, UIGestureRecognizerDelegate>
@property (strong, nonatomic) UIView *preview;
Expand All @@ -28,6 +29,8 @@ @interface LLSimpleCamera () <AVCaptureFileOutputRecordingDelegate, UIGestureRec
@property (nonatomic, assign) CGFloat beginGestureScale;
@property (nonatomic, assign) CGFloat effectiveScale;
@property (nonatomic, copy) void (^didRecordCompletionBlock)(LLSimpleCamera *camera, NSURL *outputFileUrl, NSError *error);
@property (assign, nonatomic) UIDeviceOrientation lastDeviceOrientation;

@end

NSString *const LLSimpleCameraErrorDomain = @"LLSimpleCameraErrorDomain";
Expand Down Expand Up @@ -81,6 +84,8 @@ - (void)setupWithQuality:(NSString *)quality
_recording = NO;
_zoomingEnabled = YES;
_effectiveScale = 1.0f;
_lastDeviceOrientation = UIDeviceOrientationPortrait;

}

- (void)viewDidLoad
Expand Down Expand Up @@ -111,6 +116,24 @@ - (void)viewDidLoad
[self addDefaultFocusBox];
}

- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
__weak typeof(self) weakSelf = self;
[[DBMotionManager sharedManager] setMotionRotationHandler:^(UIDeviceOrientation orientation){
[weakSelf rotationChanged:orientation];
}];
[[DBMotionManager sharedManager] startMotionHandler];
}

- (void) rotationChanged:(UIDeviceOrientation) orientation
{
if ( orientation != UIDeviceOrientationUnknown ||
orientation != UIDeviceOrientationFaceUp ||
orientation != UIDeviceOrientationFaceDown ) {
_lastDeviceOrientation = orientation;
}
}

#pragma mark Pinch Delegate

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
Expand Down Expand Up @@ -779,7 +802,7 @@ - (AVCaptureVideoOrientation)orientationForConnection
AVCaptureVideoOrientation videoOrientation = AVCaptureVideoOrientationPortrait;

if(self.useDeviceOrientation) {
switch ([UIDevice currentDevice].orientation) {
switch (self.lastDeviceOrientation) {
case UIDeviceOrientationLandscapeLeft:
// yes to the right, this is not bug!
videoOrientation = AVCaptureVideoOrientationLandscapeRight;
Expand Down
12 changes: 11 additions & 1 deletion LLSimpleCameraExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
17E4C4B51A0123C100E61ACD /* UIImage+Resize.m in Sources */ = {isa = PBXBuildFile; fileRef = 17E4C4B21A0123C100E61ACD /* UIImage+Resize.m */; };
17E4C4B91A01285800E61ACD /* HomeViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 17E4C4B81A01285800E61ACD /* HomeViewController.m */; };
17E4C4BC1A0128EC00E61ACD /* ViewUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 17E4C4BB1A0128EC00E61ACD /* ViewUtils.m */; };
56F0FDCA1FE3DB4B00D25A24 /* DBMotionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 56F0FDC91FE3DB4B00D25A24 /* DBMotionManager.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -76,6 +77,8 @@
17E4C4B81A01285800E61ACD /* HomeViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HomeViewController.m; sourceTree = "<group>"; };
17E4C4BA1A0128EC00E61ACD /* ViewUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewUtils.h; sourceTree = "<group>"; };
17E4C4BB1A0128EC00E61ACD /* ViewUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewUtils.m; sourceTree = "<group>"; };
56F0FDC81FE3DB4B00D25A24 /* DBMotionManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DBMotionManager.h; sourceTree = "<group>"; };
56F0FDC91FE3DB4B00D25A24 /* DBMotionManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DBMotionManager.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -173,6 +176,8 @@
17E4C4A81A01235200E61ACD /* LLSimpleCamera */ = {
isa = PBXGroup;
children = (
56F0FDC81FE3DB4B00D25A24 /* DBMotionManager.h */,
56F0FDC91FE3DB4B00D25A24 /* DBMotionManager.m */,
171362191A0144100034A6FE /* UIImage+FixOrientation.h */,
1713621A1A0144100034A6FE /* UIImage+FixOrientation.m */,
17E4C4A91A01235200E61ACD /* LLSimpleCamera.h */,
Expand Down Expand Up @@ -258,7 +263,7 @@
TargetAttributes = {
17E4C47E1A0122A700E61ACD = {
CreatedOnToolsVersion = 6.0.1;
DevelopmentTeam = NZPAC7Q696;
DevelopmentTeam = 9HZCPHT288;
};
17E4C4971A0122A700E61ACD = {
CreatedOnToolsVersion = 6.0.1;
Expand Down Expand Up @@ -323,6 +328,7 @@
1713621B1A0144100034A6FE /* UIImage+FixOrientation.m in Sources */,
17E4C4BC1A0128EC00E61ACD /* ViewUtils.m in Sources */,
17E4C4881A0122A700E61ACD /* AppDelegate.m in Sources */,
56F0FDCA1FE3DB4B00D25A24 /* DBMotionManager.m in Sources */,
17E4C4851A0122A700E61ACD /* main.m in Sources */,
17E4C4B91A01285800E61ACD /* HomeViewController.m in Sources */,
1733A8EE1C791BA5000B3648 /* LLSimpleCamera+Helper.m in Sources */,
Expand Down Expand Up @@ -442,9 +448,11 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = 9HZCPHT288;
INFOPLIST_FILE = LLSimpleCameraExample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.szy.seebaby.test;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
};
Expand All @@ -456,9 +464,11 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = 9HZCPHT288;
INFOPLIST_FILE = LLSimpleCameraExample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.szy.seebaby.test;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
};
Expand Down
3 changes: 2 additions & 1 deletion LLSimpleCameraExample/HomeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ - (void)viewDidLoad

// read: http://stackoverflow.com/questions/5427656/ios-uiimagepickercontroller-result-image-orientation-after-upload
// you probably will want to set this to YES, if you are going view the image outside iOS.
self.camera.fixOrientationAfterCapture = NO;
self.camera.fixOrientationAfterCapture = YES;
self.camera.useDeviceOrientation = YES;

// take the required actions on a device change
__weak typeof(self) weakSelf = self;
Expand Down
2 changes: 1 addition & 1 deletion LLSimpleCameraExample/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>com.omerfarukgul.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down