iOS 如何获取截屏图片并且展示出来呢?

       现在很多iOS应用涉及到活动什么的分享什么的用到把当前截屏图片放置页面的对应位置,所以今天九聊一聊如何将截屏图片展示到APP对应页面中去呢,废话不多,先上代码。

1.      首先,我们需要一个监听截图行为的通知,格式如:

[[NSNotificationCenter defaultCenter] addObserver:self

    selector:@selector(userDidTakeScreenshot:)

    name:UIApplicationUserDidTakeScreenshotNotification object:nil];

2.   补充截屏通知对应的方法:

//截屏响应- (void)userDidTakeScreenshot:(NSNotification *)notification

{

    NSLog(@"检测到截屏");


    //人为截屏, 模拟用户截屏行为, 获取所截图片(下面第三布进行实现)

UIImage *image_ = [self imageWithScreenshot];


    //添加显示UIImageView *imgvPhoto = [[UIImageView alloc]initWithImage:image_];

    imgvPhoto.frame = CGRectMake(self.window.frame.size.width/2, self.window.frame.size.height/2, self.window.frame.size.width/2, self.window.frame.size.height/2);


    //添加边框CALayer * layer = [imgvPhoto layer];

    layer.borderColor = [

        [UIColor whiteColor] CGColor];

    layer.borderWidth =5.0f;

    //添加四个边阴影imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor;

    imgvPhoto.layer.shadowOffset = CGSizeMake(0,0);

    imgvPhoto.layer.shadowOpacity =0.5;

    imgvPhoto.layer.shadowRadius =10.0;

    //添加两个边阴影imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor;

    imgvPhoto.layer.shadowOffset = CGSizeMake(4,4);

    imgvPhoto.layer.shadowOpacity =0.5;

    imgvPhoto.layer.shadowRadius =2.0;

    [self.window addSubview:imgvPhoto];

}

3.  具体实现截屏图片的获取

- (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 *windowin [[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);

        }

        elseif(orientation == UIInterfaceOrientationLandscapeRight)

        {

            CGContextRotateCTM(context, -M_PI_2);

            CGContextTranslateCTM(context, -imageSize.height,0);

        } elseif(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);

}


//将NSData格式转成图片

- (UIImage *)imageWithScreenshot

{

    NSData *imageData = [self dataWithScreenshotInPNGFormat];

    return [UIImage imageWithData:imageData];

}

其实,实现这个效果很简单记得两个重点环节:1.截屏时系统会返回给我们一个通知;2.drawViewHierarchyInRect:afterScreenUpdates这个方法会将截到的图给对应的view展示,然后就是做个防崩措施 

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

推荐阅读更多精彩内容