zoukankan      html  css  js  c++  java
  • iOS 截屏分享(包含状态栏与不包含状态栏)


    iOS8以上的新方法PhotoKit

    监听截图相册变化,取最后一张图片:http://www.hangge.com/blog/cache/detail_1515.html

    PhotoKit 获取本机相册列表 PHAssetCollection有一个名为 Screenshots的智能相册,获取这里面最新的一张图片,即可获取用户刚刚截屏的图片

    自己截取屏幕,兼容iOS7-iOS10:带状态栏

    • OTScreenshotHelper oc 开源库
      https://github.com/OpenFibers/OTScreenshotHelper

    • 直接使用 OTScreenshotHelper 截图效果如下:
      不知道为什么头部和底部的view是透明的,上下两个view都设置了毛玻璃效果,但是截图出来view背景透明
      image_1bludrnbpre877i1ehfa4m19td9.png-221.9kB

    • 目前解决方案是不带状态栏的,所以试过各种方案之后还是两个方案融合在一起:使用OTScreenshotHelper截图状态栏, 使用不带状态栏方案截取主体部分

    1. 刚刚拷贝源码到swift项目

    • UIView+OTStatusBarReference.m
    #import "UIView+ComOpenThreadOTScreenshotHelperStatusBarReference.h"
    #import "ComOpenThreadOTScreenshotHelperSwizzleHelper.h"
    // 这一行需要移动到 import 语句下面,不然会报库找不到
    static UIView *statusBarInstance = nil;
    
    • OTScreenShotHelper.h
    把#import <Foundation/Foundation.h> 
    增加:
    #import <UIKit/UIKit.h>
    解决UIView 提示找不到的问题
    

    2. OTScreenShotHelper.h 中暴露mergeStatusBarToContext方法

    这个方法在OTScreenShotHelper 其实已经实现了,就是没有暴露给外面调用

    + (void)mergeStatusBarToContext:(CGContextRef)context rect:(CGRect)rect screenshotOrientation:(UIInterfaceOrientation)o;
    

    3. 在现有截图代码位置调用mergeStatusBarToContext

    for subWindow in (UIApplication.shared.windows) {
    context.saveGState()
    context.translateBy(x: subWindow.center.x, y: subWindow.center.y)
    context.concatenate(subWindow.transform)
    context.translateBy(x: -subWindow.bounds.size.width * subWindow.layer.anchorPoint.x,
    y: -subWindow.bounds.size.height * subWindow.layer.anchorPoint.y)
    if (orientation == UIInterfaceOrientation.landscapeLeft) {
    context.rotate(by: CGFloat(M_PI_2))
    context.translateBy(x: 0, y: -imageSize.width)
    }
    else if (orientation == UIInterfaceOrientation.landscapeRight) {
    context.rotate(by: -CGFloat(M_PI_2))
    context.translateBy(x: -imageSize.width, y: 0)
    }
    else if (orientation == UIInterfaceOrientation.portraitUpsideDown) {
    context.rotate(by: CGFloat(M_PI))
    context.translateBy(x: -imageSize.width, y: -imageSize.height)
    }
    if ( subWindow.responds(to: #selector(UIWindow.drawHierarchy(in:afterScreenUpdates:))) ){
    subWindow.drawHierarchy(in: subWindow.bounds, afterScreenUpdates: true)
    } else {
    subWindow.layer.render(in: context)
    }
    context.restoreGState()
    
    // 这部分是截图带有statusbar的关键,在这里把状态栏截图动态添加到了原有图片中,在未带状态栏截图代码如下位置添加如下代码即可
    let statusBarRect = CGRect(x: 0, y: 0,  AppWidth, height: 20)
    OTScreenshotHelper.mergeStatusBar(to: context, rect: statusBarRect, screenshotOrientation: orientation)
    // End
    
    }
    
    let image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image
    

    未带状态栏截图代码参见:
    https://gist.github.com/buoge/a94798cd49854a569fdc32825c015167


    自己截取屏幕,兼容iOS7-iOS10:不带状态栏

    image_1blue6gp9u7d1svtr38hug1m9im.png-119.7kB

    创建两个UIImage
    UIImage *image1 = [UIImage imageNamed:@"iOSDevTip"];
    UIImage *image2 = [UIImage imageNamed:@"CodePush"];
    创建UIImage的方法有很多种,我们就简单的通过imageNamed:方法来创建。
    
    合并之后的size
    CGSize size = CGSizeMake(image1.size.width + image2.size.width, image1.size.height);
    
    合并两个UIImage,需要计算合并之后的size。假设这两个UIImage的高度是是相同的,把他们的宽度相加,得到合并之后的UIImage的size。
    合并方法有了UIImage和size接下来就是把两个UIImage合并,方法如下:
    
    UIGraphicsBeginImageContext(size);
    [image1 drawInRect:CGRectMake(0, 0, image1.size.width, size.height)];
    [image2 drawInRect:CGRectMake(image1.size.width, 0, image2.size.width, size.height)];
    UIImage *togetherImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
  • 相关阅读:
    图像分割学习笔记_1(opencv自带meanshift分割例子)
    基础学习笔记之opencv(10):Mat mask操作
    Qt学习之路_6(Qt局域网聊天软件)
    Qt学习之路_8(Qt中与文件目录相关操作)
    Android开发历程_6(RadioButton和CheckBox的使用)
    Qt学习之路_4(Qt UDP的初步使用)
    目标跟踪学习笔记_4(particle filter初探3)
    Reading papers_11(读Integrating local action elements for action analysis相关文章)
    基础学习笔记之opencv(8):Mat 基本图像容器
    基础学习笔记之opencv(14):随机数发生器&绘制文字
  • 原文地址:https://www.cnblogs.com/buoge/p/9343270.html
Copyright © 2011-2022 走看看