zoukankan      html  css  js  c++  java
  • ios截屏事件监听

    目的:实现截屏反馈,类似支付宝的截屏上传反馈功能.

    1.注册全局通知,在Appdelegate中注册截屏监听通知

    - (void)registNotification{
        
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getScreenshot:) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
       
    }
    

      

    2.实现通知

    - (void)getScreenshot:(NSNotification *)notification{
        NSLog(@"捕捉截屏事件");
        UIImage *shorImg =  [UIImage imageWithData:[self imageDataScreenShot]];
    
    }
    

      

    - (NSData *)imageDataScreenShot
    {
        CGSize imageSize = CGSizeZero;
        imageSize = [UIScreen mainScreen].bounds.size;
        
        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 ([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);
    }
    

      

  • 相关阅读:
    C++中智能指针的设计和使用
    [转]C++ 智能指针详解
    C++ const 常量和常指针
    深入理解C++中的mutable关键字
    C++ 静态常量
    BZOJ 1875: [SDOI2009]HH去散步
    BZOJ 1024: [SCOI2009]生日快乐
    BZOJ 1059: [ZJOI2007]矩阵游戏
    bzoj 1833: [ZJOI2010]count 数字计数
    LUOGU P2587 [ZJOI2008]泡泡堂
  • 原文地址:https://www.cnblogs.com/likun123/p/9518413.html
Copyright © 2011-2022 走看看