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 即为查找到的图片,也是此时的截屏图片
        }
    }];
}

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

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,035评论 25 709
  • 想必使用iPhone的用户, 大家都知道按照Home键+电源键就可以截屏了。 截屏对于产品经理、工程师、设计师都比...
    Startry阅读 12,539评论 21 44
  • 转载://www.greatytc.com/p/aa64fd3dd621 想必使用iPhone的用户, 大...
    晓飞90阅读 5,114评论 0 1
  • 张清的日精进第62天 体验入 今天是销售部的四季度冲刺启动会。明显发现参加过学习的同事个个不甘人后,积极互生,勇于...
    kiyoi2017阅读 786评论 0 2
  • 知道他这个人是在初三上学期,当时对他的印象只有,噢,转校生,长的还行,我讨厌的女生喜欢他。大家都说他长的很好,只...
    拒撩阅读 2,790评论 1 0