• iOS开发 代码 或 <Home+Power>截屏


    1. 截屏的两种简单方法,

    **注意这两种截图方法,都必须在视图完全加载完成后才能截图,即在 viewDidAppear 方法之后截屏,否则无法得到想要的截屏效果
    **
    (1) 利用绘图方法 renderInContext

    /**
     *  截取当前屏幕的内容
     */
    - (void)snapshotScreen
    {
        // 判断是否为retina屏, 即retina屏绘图时有放大因子
        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]){
            UIGraphicsBeginImageContextWithOptions(self.view.window.bounds.size, NO, [UIScreen mainScreen].scale);
        } else {
            UIGraphicsBeginImageContext(self.view.window.bounds.size);
        }
        [self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    // 保存到相册
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }
    
    

    该方法可以更改为截取某个视图的内容

    /**
     *  截取某视图的内容
     */
    - (void)snapshotScreenInView:(UIView *)view
    {
        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]){
            UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
        } else {
            UIGraphicsBeginImageContext(view.bounds.size);
        }
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
        
    }
    
    

    (2) . 利用iOS 7之后UIView提供的方法 drawViewHierarchyInRect: afterScreenUpdates:

    /**
     *  截取一个contentView的内容
     *
     *  @param contentView
     */
    - (void)snapshotScreenInView:(UIView *)contentView {
        CGSize size = contentView.bounds.size;
        UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
        CGRect rect = contentView.frame;
        //  自iOS7开始,UIView类提供了一个方法-drawViewHierarchyInRect:afterScreenUpdates: 它允许你截取一个UIView或者其子类中的内容,并且以位图的形式(bitmap)保存到UIImage中
        [contentView drawViewHierarchyInRect:rect afterScreenUpdates:YES];
        
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }
    

    当然以上两种方法,我还没办法截屏到Status Bar(时间,网络状态等),如果截图中需要这些信息,就可以通过<Home+Power键>来截屏获取截屏图片

    2 . 监听手机<Home + Power>键截屏,并获取截屏图片

    (1) . 在合适的位置给应用添加监听,监听手机截屏动作

    /**
     *  为<Home + Power键>添加监听
     *  selector 为监听到截屏后调用的方法
     */
    - (void) addScreenshotObserverWithSlelctor:(SEL)selector {
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(selector)
                                                     name:UIApplicationUserDidTakeScreenshotNotification object:nil];
        
    }
    
    

    (2) . 监听到截屏在相册中查找最新的一张图片即为截屏图片,添加 #import <Photos/Photos.h>

    /**
     *  相册中最新的一张图片
     */
    - (void)latestAssetImage {
      
        // 获取所有资源的集合,并按资源的创建时间排序
        PHFetchOptions *options = [[PHFetchOptions alloc] init];
        options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
        PHFetchResult *assetsFetchResults = [PHAsset fetchAssetsWithOptions:options];
        PHAsset *asset = [assetsFetchResults firstObject];
        // 使用PHImageManager从PHAsset中请求图片
        PHImageManager *imageManager = [[PHImageManager alloc] init];
        [imageManager requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
            if (result) {
               // result 即为查找到的图片,也是此时的截屏图片
            }
        }];
    }
    
    
  • 相关阅读:
    iis6|iis7|配置URLRewriter|64位操作系统下|.net2.0|.net4.0|配置URLRewriter|Web.config配置详情
    如何用Fiddler对Android应用进行抓包
    asp.net写验证码
    Linq to sql 语法方法示例
    asp.net正则表达式学习例子
    js 根据年月获取当月有多少天_js获取农历日期_及Js其它常用有用函数
    Sql Server 相关错误问题及解决方法
    Javascript 添加自定义静态方法属性JS清除左右空格
    asp.net 文件批量移动重命名
    Memcached监听多个端口_同一台Windows机器中启动多个Memcached服务
  • 原文地址:https://www.cnblogs.com/jaesun/p/iOS-kai-fa-dai-ma-huo-HomePowerjie-ping.html
Copyright © 2020-2023  润新知