相关链接
代码相关
* 点击拍照按钮的一瞬间获取当前的重力方向,获取一次方向直接关掉定时器,不然手机会发热
#import <CoreMotion/CoreMotion.h> // 导入头
@property (nonatomic,strong) CMMotionManager *mManager; // 重力感应管理
// 重力感应管理懒加载
- (CMMotionManager *)mManager {
if (!_mManager) {
_mManager = [[CMMotionManager alloc] init];
}
return _mManager;
}
/**
* 开启重力感应
*/
+ (void)startUpdateAccelerometerWithManager:(CMMotionManager *)manager Result:(void (^)(UIImageOrientation direction))result;
+ (void)startUpdateAccelerometerWithManager:(CMMotionManager *)manager Result:(void (^)(UIImageOrientation direction))result {
if ([manager isAccelerometerAvailable] == YES) {
//回调会一直调用,建议获取到就调用下面的停止方法,需要再重新开始,当然如果需求是实时不间断的话可以等离开页面之后再stop
NSTimeInterval updateInterval = 1/15.0;
[manager setAccelerometerUpdateInterval:updateInterval];
[manager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error)
{
double x = accelerometerData.acceleration.x;
double y = accelerometerData.acceleration.y;
if (fabs(y) >= fabs(x))
{
if (y >= 0){
//Down
NSLog(@"倒立");
result(UIImageOrientationDown);
}
else{
//Portrait
NSLog(@"正常");
result(UIImageOrientationUp);
}
}
else
{
if (x >= 0){
//Right
NSLog(@"右边");
result(UIImageOrientationRight);
}
else{
//Left
NSLog(@"左边");
result(UIImageOrientationLeft);
}
}
[self stopUpdateWithManager:manager]; // 调用一次直接关闭
}];
}
}
#pragma mark 关闭更新
+ (void)stopUpdateWithManager:(CMMotionManager *)manager {
if ([manager isAccelerometerActive] == YES) {
NSLog(@"停止");
[manager stopAccelerometerUpdates];
manager = nil;
}
}
* 直接根据重力感应获取的方向来调整排到的图片的旋转角度
/**
* 处理图片方向
*/
+ (UIImage *)image:(UIImage *)image rotation:(UIImageOrientation)orientation;
#pragma mark 图片处理部分
+ (UIImage *)image:(UIImage *)image rotation:(UIImageOrientation)orientation {
switch (orientation) {
case UIImageOrientationLeft: {
UIImage *newImage = [UIImage imageWithCGImage:image.CGImage scale:1.0 orientation:UIImageOrientationUp];
return newImage;
break;
}
case UIImageOrientationRight:{
// 调整好
UIImage *newImage = [UIImage imageWithCGImage:image.CGImage scale:1.0 orientation:UIImageOrientationDown];
return newImage;
break;
}
case UIImageOrientationDown:{
// 这个是对的
UIImage *newImage = [UIImage imageWithCGImage:image.CGImage scale:1.0 orientation:UIImageOrientationLeft];
return newImage;
break;
}
default:
return image;
break;
}
return image;
}