iOS自定义弹窗alert、sheet时的摇一摇截屏处理

场景由于各种产品需求的变动,我们在项目里一般情况下会用系统的UIAlertController,当然有很多特别的需求,所以我们就定义了一些view来做成美工需求的样式,还有些我们定义了一个AlertControllerUIAlertController相似度特别高,那么问题来了,截屏时,有些只能截屏到弹窗,还有只能截屏到弹窗下面的那个控制器的view。想来想去,只能把所有窗口从后往前一直截屏然后叠加,但是用全英文在谷歌没有搜索到适合的东西,好在同事用汉字在百度找到了可用的代码。记录一下。有时候遇到问题,我们可以适当地切换一下谷歌百度,适当地切换一下中文关键字和英文关键字。

截取当前屏幕内容 将以下代码粘贴复制 直接调用imageWithScreenshot方法

/**
 *  截取当前屏幕
 *
 *  @return NSData *
 */
- (NSData *)dataWithScreenshotInPNGFormat
{
    CGSize imageSize = CGSizeZero;
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (UIInterfaceOrientationIsPortrait(orientation))
        imageSize = [UIScreen mainScreen].bounds.size;
    else
        imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
    
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    for (UIWindow *window in [[UIApplication sharedApplication] windows])
    {
        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 (orientation == UIInterfaceOrientationLandscapeLeft)
        {
            CGContextRotateCTM(context, M_PI_2);
            CGContextTranslateCTM(context, 0, -imageSize.width);
        }
        else if (orientation == UIInterfaceOrientationLandscapeRight)
        {
            CGContextRotateCTM(context, -M_PI_2);
            CGContextTranslateCTM(context, -imageSize.height, 0);
        } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
            CGContextRotateCTM(context, M_PI);
            CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
        }
        if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
        {
            [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
        }
        else
        {
            [window.layer renderInContext:context];
        }
        CGContextRestoreGState(context);
    }
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return UIImagePNGRepresentation(image);
}

/**
 *  返回截取到的图片
 *
 *  @return UIImage *
 */
- (UIImage *)imageWithScreenshot
{
    NSData *imageData = [self dataWithScreenshotInPNGFormat];
    return [UIImage imageWithData:imageData];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,556评论 25 708
  • 101页读到121
    汪小霖阅读 217评论 0 0
  • 这两天我送妞回外公家了,老公也要出差,就儿子一个人在家,本来让他去小叔家住两天,可他不愿意。说自己一个人在家...
    國國2580阅读 181评论 0 0
  • 明月今天一天都不顺心,早上时候闹钟响了,明月实在困得不行就没有起来做饭,先生就抱怨了几句,两人就吵了几句。 先生上...
    利萍阅读 429评论 0 0
  • 文/ 麦醺 大学毕业后,我漂在西北的一个三线城市里。当时刚上班的我和同事合租在一个50平...
    麦小醺阅读 790评论 2 3