IOS 截屏||截图

来源:互联网 发布:流星网络电视tv版apk 编辑:程序博客网 时间:2024/05/01 20:02
#pragma mark - 截屏方法
/**
 *@return UIImage
 */
+(UIImage *)ScreenShots
{
    CGSize imageSize = [[UIScreen mainScreen] bounds].size;
    if (NULL != UIGraphicsBeginImageContextWithOptions) {
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    }
    else
    {
        UIGraphicsBeginImageContext(imageSize);
    }
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    UIImage *image = nil;
    for (UIWindow * window in [[UIApplication sharedApplication] windows]) {
        if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) {
            CGContextSaveGState(context);
            CGContextTranslateCTM(context, [window center].x, [window center].y);
            CGContextConcatCTM(context, [window transform]);
            CGContextTranslateCTM(context, -[window bounds].size.width*[[window layer] anchorPoint].x, -[window bounds].size.height*[[window layer] anchorPoint].y);
            if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
                [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
                image = UIGraphicsGetImageFromCurrentImageContext();
            } else {
                [window.layer renderInContext:UIGraphicsGetCurrentContext()];
                image = UIGraphicsGetImageFromCurrentImageContext();
            }
            
            CGContextRestoreGState(context);
        }
    }
    
    UIGraphicsEndImageContext();
    
    DLog(@"Suceeded!");
    return image;

}



//截图  ,将下面的window改成你要截图的对象即可

-(UIImage *)captureAndSaveView:(UIView *)view{
    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 1.0);  //NO,YES 控制是否透明
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = nil;
    if ([view respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
        [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
        image = UIGraphicsGetImageFromCurrentImageContext();
    } else {
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        image = UIGraphicsGetImageFromCurrentImageContext();
    }

    UIGraphicsEndImageContext();
    return image;
}

0 0
原创粉丝点击