ios 设备方向,屏幕旋转检测与图片方向.

这次是由于获取整个应用程序截图.然后上传服务端,在不同的机器上出现的bug.
介绍下具体步骤:
1.获取整个程序的截图

UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
UIGraphicsBeginImageContextWithOptions(screenWindow.frame.size, NO, 0.0); // no ritina
[screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

2.这个时候已经获取到图片了,但是图片的方向,以及在屏幕选转得过程中都会有变化.如何检测?第一种方式获取设备的方向,这个方法其实不靠谱,设备的方向,不能决定home键的方向.具体自己测试,所以这个方法不推荐:

switch ([UIDevice currentDevice].orientation) {     
//UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
//if (UIDeviceOrientationIsLandscape(deviceOrientation)) NSLog(@"横向");
//else if(UIDeviceOrientationIsPortrait(deviceOrientation)) NSLog(@"纵向");

    case UIDeviceOrientationPortrait:
    {
        LOG(@"上");
        //image = [UIImage imageWithCGImage:image.CGImage scale:1 orientation:UIImageOrientationLeft];
    }
        break;
    case UIDeviceOrientationPortraitUpsideDown:
    {
        LOG(@"下");
        image = [UIImage imageWithCGImage:image.CGImage scale:1 orientation:UIImageOrientationDown];
    }
        break;
    case UIDeviceOrientationLandscapeLeft:
    {
        LOG(@"左");
        image = [UIImage imageWithCGImage:image.CGImage scale:1 orientation:UIImageOrientationLeft];
    }
        break;
    case UIDeviceOrientationLandscapeRight:
    {
        LOG(@"右");
        image = [UIImage imageWithCGImage:image.CGImage scale:1 orientation:UIImageOrientationRight];
        
    }
        break;
    case UIDeviceOrientationFaceUp:
    {
        LOG(@"面向上");
        //image = [UIImage imageWithCGImage:image.CGImage scale:1 orientation:UIImageOrientationRight];
        
    }
        break;
        
    default:
    break;
}

3.使用这个UIInterfaceOrientation (The orientation of the app's user interface.)我理解的就是程序界面的方向,这个可以知道当前屏幕的状态.知道Home键的放向就比较好处理了.

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
switch (orientation) {
    case UIInterfaceOrientationLandscapeRight:
    {
        LOG(@"右");
        image = [UIImage imageWithCGImage:image.CGImage scale:1 orientation:UIImageOrientationLeft];
    }
        break;
    case UIInterfaceOrientationLandscapeLeft:
    {
        LOG(@"左");
        image = [UIImage imageWithCGImage:image.CGImage scale:1 orientation:UIImageOrientationRight];
    }
        break;
    case UIInterfaceOrientationPortraitUpsideDown:
    {
        LOG(@"上");
        image = [UIImage imageWithCGImage:image.CGImage scale:1 orientation:UIImageOrientationDown];
    }
        break;
    case UIInterfaceOrientationPortrait:
    {
        LOG(@"下");
        image = [UIImage imageWithCGImage:image.CGImage scale:1 orientation:UIImageOrientationUp];
    }
        break;
    case UIInterfaceOrientationUnknown:
    {
        LOG(@"不知道");
    }
        break;
        
    default:
        break;
}

4.最后就要根据不同的设备进行适配了,这里要获取设备类型,要具体的,ipad2.1,还是ipad3.4 ,不同设备截取的图片都不一样.这里是获取设备信息的代码
+ (NSString *)platform {
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platformName = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
free(machine);

return platformName;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,896评论 18 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,200评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,228评论 4 61
  • 使用场景:只需要一个实例。例如现实世界的资源:服务器;或封装共享资源。 意义: 1、使得创建的实例始终保持一份,避...
    关灯侠阅读 100评论 0 0
  • 一个暑假,你可以选择听音乐,把各种风格的音乐歌曲听个遍,然后选择几首适合自己的,或者自己喜欢的,把它翻唱到极致;也...
    狸小笨阅读 556评论 3 2