IOS屏幕截图的记录

作者: 司马捷 | 来源:发表于2015-08-24 17:53 被阅读1464次

虽然网上有很多不同的方法,但我还是写出来我自己写的一套,哈哈~
这段代码前前后后,写了一个多月,修修改改.中间测试的时候,总是出现很多bug.直到现在这个终极版.
写出好的代码,真的不容易.
1.这个是截取window上的代码:

UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
UIGraphicsBeginImageContextWithOptions(screenWindow.frame.size, NO, 0.0); // no ritina

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSaveGState(context);
for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
    
    if(window == screenWindow)
    {
            break;
    }else{
        [window.layer renderInContext:context];
    }
}

//  //  ////////////////////////
if ([screenWindow respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
    [screenWindow drawViewHierarchyInRect:screenWindow.bounds afterScreenUpdates:YES];
} else {
    [screenWindow.layer renderInContext:context];
}
CGContextRestoreGState(context);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
screenWindow.layer.contents = nil;
UIGraphicsEndImageContext();

float iOSVersion = [UIDevice currentDevice].systemVersion.floatValue;
if(iOSVersion < 8.0)
{
    image = [self rotateUIInterfaceOrientationImage:image];
}

然后是上面代码中引用的一个方法,我自己写的,当屏幕旋转的时候,对图片进行一个旋转.

-(UIImage *)rotateUIInterfaceOrientationImage:(UIImage *)image{

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;
}

return image;
}

相关文章

网友评论

  • 张云龙:我用一个view的layer播放视频,但是截图后是黑的,没有视频。
    司马捷:@张云龙 你要获取这个layer,对一个layer进行渲染.
    - (UIImage *)captureScreenInRect:(CGRect)captureFrame {
    CALayer *layer;
    layer = self.view.layer;
    UIGraphicsBeginImageContext(self.view.bounds.size);
    CGContextClipToRect (UIGraphicsGetCurrentContext(),captureFrame);
    [layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return screenImage;
    }
    张云龙:@机器人小雪 我这个情况太特殊了,找了国内外好多网站也找不到解决方案,我用AVPlayer自定义了一个AVLpalyerLayer播放M3U8,无论是截图还是你给我的这个都不行,头疼死了。
    司马捷:@张云龙 这个不行,你试试这个 http://www.it165.net/pro/html/201410/23466.html
  • 永远都能:佩服啊啊啊啊啊 解决了我的问题 网上的

本文标题:IOS屏幕截图的记录

本文链接:https://www.haomeiwen.com/subject/eixqcttx.html