这次是由于获取整个应用程序截图.然后上传服务端,在不同的机器上出现的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;
}