写在前面

最近有这么一个需求,分享页面,分享的是web订单截图,既然是web 就会有超出屏幕的部分,
生成的图片还要加上我们的二维码,这就涉及到图片的合成了。
有了这样的需求,就是各种google.也算解决了自己的需求,另外,也总结了一下,对需求做了下拓展,目前是swift4.0版本。

整合成一个三方库,以下只是部分代码,详细代码及demo请见,github地址https://github.com/dudongge/DDGScreenShot


View生成图片

代码也比较简单 screenShotImage 就是截取后的图片
let context = UIGraphicsGetCurrentContext()
context?.saveGState()
context?.translateBy(x: -self.frame.origin.x, y: -self.frame.origin.y);
self.layer.render(in: context!)
let screenShotImage = UIGraphicsGetImageFromCurrentImageContext()
context?.restoreGState();
UIGraphicsEndImageContext()

ScrollView生成图片

只要实现原理是计算偏移量,每一屏绘制一次,放在内存里,最后将所有的图片组合成一张图片 screenShotImage就是最终图片
public func DDGContentScrollScreenShot (_ completionHandler: @escaping (_ screenShotImage: UIImage?) -> Void) { self.isShoting = true let snapShotView = self.snapshotView(afterScreenUpdates: true)
snapShotView?.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: (snapShotView?.frame.size.width)!, height: (snapShotView?.frame.size.height)!)
self.superview?.addSubview(snapShotView!) let bakOffset = self.contentOffset let page = floorf(Float(self.contentSize.height / self.bounds.height)) UIGraphicsBeginImageContextWithOptions(self.contentSize, false, UIScreen.main.scale) self.DDGContentScrollPageDraw(0, maxIndex: Int(page), drawCallback: { [weak self] () -> Void in
let strongSelf = self let screenShotImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext() strongSelf?.setContentOffset(bakOffset, animated: false)
snapShotView?.removeFromSuperview() strongSelf?.isShoting = false completionHandler(screenShotImage)
}) } fileprivate func DDGContentScrollPageDraw (_ index: Int, maxIndex: Int, drawCallback: @escaping () -> Void) { self.setContentOffset(CGPoint(x: 0, y: CGFloat(index) * self.frame.size.height), animated: false)
let splitFrame = CGRect(x: 0, y: CGFloat(index) * self.frame.size.height, width: bounds.size.width, height: bounds.size.height) DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(0.3 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)) { () -> Void in
self.drawHierarchy(in: splitFrame, afterScreenUpdates: true) if index < maxIndex {
self.DDGContentScrollPageDraw(index + 1, maxIndex: maxIndex, drawCallback: drawCallback)
}else{
drawCallback()
}
}
}

UIwebView生成图片 && wkwebView

绘制时大同小异,只是wkwebView 调用的绘制方法为:drawHierarchy 其核心代码如下
采用递归,直到拿到最后一个偏移量。
fileprivate func DDGRenderImageView(_ completionHandler: @escaping (_ screenShotImage: UIImage?) -> Void) {
let ddgTempRenderView = UIView(frame: CGRect(x: 0, y: 0, width: self.contentSize.width, height: self.contentSize.height))
self.removeFromSuperview()
ddgTempRenderView.addSubview(self) self.contentOffset = CGPoint.zero
self.frame = ddgTempRenderView.bounds DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(0.3 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)) { () -> Void in
let bounds = self.bounds
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale) if (self.DDGContainsWKWebView()) {
self.drawHierarchy(in: bounds, afterScreenUpdates: true)
}else{
self.layer.render(in: UIGraphicsGetCurrentContext()!)
}
let screenShotImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext() completionHandler(screenShotImage)
}
}
fileprivate func shotScreenContentScrollPageDraw (_ index: Int, maxIndex: Int, drawCallback: @escaping () -> Void) { self.scrollView.setContentOffset(CGPoint(x: 0, y: CGFloat(index) * self.scrollView.frame.size.height), animated: false)
let splitFrame = CGRect(x: 0, y: CGFloat(index) * self.scrollView.frame.size.height, width: bounds.size.width, height: bounds.size.height) DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(0.3 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)) { () -> Void in
self.drawHierarchy(in: splitFrame, afterScreenUpdates: true) if index < maxIndex {
self.shotScreenContentScrollPageDraw(index + 1, maxIndex: maxIndex, drawCallback: drawCallback)
}else{
drawCallback()
}
}
}

两张图片合为一张(一张底图,一张logo)

在UIImage上做了拓展
let imageRef = self.cgImage
let w: CGFloat = CGFloat((imageRef?.width)!)
let h: CGFloat = CGFloat((imageRef?.height)!)
//以1.png的图大小为画布创建上下文
UIGraphicsBeginImageContext(CGSize(width: w, height: h))
self.draw(in: CGRect(x: 0, y: 0, width: w, height: h))
//先把1.png 画到上下文中
logo.draw(in: CGRect(x: logoOrigin.x,
y: logoOrigin.y,
width: logoSize.width,
height:logoSize.height))
//再把小图放在上下文中
let resultImg: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
//从当前上下文中获得最终图片
UIGraphicsEndImageContext()
return resultImg!

在图片上写文字/加标签

同样对UIimage 做了拓展,增加了几个参数
public func drawTextInImage(text: String,
textColor: UIColor,
textFont: CGFloat,
textBgColor: UIColor,
textX: CGFloat,
textY: CGFloat )->UIImage {
//开启图片上下文
UIGraphicsBeginImageContext(self.size)
//图形重绘
self.draw(in: CGRect.init(x: 0, y: 0, width: self.size.width, height: self.size.height))
//水印文字属性
let att = [NSAttributedStringKey.foregroundColor: textColor,
NSAttributedStringKey.font: UIFont.systemFont(ofSize: textFont),
NSAttributedStringKey.backgroundColor: textBgColor]
//水印文字大小
let text = NSString(string: text)
let size = text.size(withAttributes: att)
//绘制文字
text.draw(in: CGRect.init(x: textX, y: textY, width: size.width, height: size.height), withAttributes: att)
//从当前上下文获取图片
let image = UIGraphicsGetImageFromCurrentImageContext()
//关闭上下文
UIGraphicsEndImageContext()
return image!
}

结束语

空间有限,所注代码不全,我把上述功能整理成了最新的带三方库,
以后会加上图片的相关处理,比如美颜,剪切,马赛克,组图等,
附上我的git地址:https://github.com/dudongge/DDGScreenShot
有什么问题也可以联系我QQ:532835032
如果对您有帮助,请您不吝star一下,增加我更新的动力

附上github上READER.ME文件部分内容

## view截屏:
view.DDGScreenShot { (image) in
拿到 image
各种复杂装逼操作
、、、、
}
## ScrollView截屏:
scrollView.DDGContentScrollScreenShot { (image) in
拿到 image
各种复杂装逼操作
、、、、
}
## webView截屏:
webView.DDGContentscreenShot { (image) in
拿到 image
各种复杂装逼操作
、、、、
}
## wkwebView截屏: 方法和webView 一样,内部做了校验
webView.DDGContentscreenShot { (image) in
拿到 image
各种复杂装逼操作
、、、、
}
## image 加 logo
let image = image.composeImageWithLogo( logo: UIImage,
logoOrigin: CGPoint,
logoSize:CGSize)
传入 logo图片,logo位置 logo 大小 就可以得到一张生成好的图片
、、、、
## image 加 标签,水印,文字
let image = image.drawTextInImage(text: String,
textColor: UIColor,
textFont: CGFloat,
textBgColor: UIColor,
textX: CGFloat,
textY: CGFloat )
传入 文字、文字颜色、字体大小、背景颜色,字体起始位置 就可以得到一张生成好的带标签的图片
、、、、
注,此方法在提交pod有问题,故将方法屏蔽,有需要的可以拷贝代码,到本地 ## 使用pod
iOS 9.0+, Swift 4.0+(Compatiable)
使用pod 导入
pod 'DDGScreenShot', '~> 1.0.1'
```

欢迎查看DDGScreenShot

其余功能如下

  1. (一)DDGScreenShot — 复杂屏幕截屏(如view ScrollView webView wkwebView)
  2. (二)DDGScreenShot--iOS 图片处理--多图片拼接
  3. (三)DDGScreenShot--iOS 图片裁剪,切圆角,加边框,你还用cornerRadius,还有更高级的用法
  4. (四)DDGScreenShot—图片擦除功能
  5. (五)DDGScreenShot—截取图片的任意部分
  6. (六)DDGScreenShot —图片加各种滤镜高逼格操作
  7. (七)DDGScreenShot —图片加高斯模糊,老电影效果

DDGScreenShot — 复杂屏幕截屏(如view ScrollView webView wkwebView)的更多相关文章

  1. iOS中的截屏(屏幕截屏及scrollView或tableView的全部截屏)

    iOS中的截屏(屏幕截屏及scrollView或tableView的全部截屏) 2017.03.16 12:18* 字数 52 阅读 563评论 4喜欢 2 1. 截取屏幕尺寸大小的图片并保存至相册 ...

  2. java实现屏幕截屏功能

    最近在项目中遇到这样一个需求,用户生成推广海报想要发送给朋友,但是推广海报是用html网页写的,这时候想要分享给朋友的话只能用户自己手机截图,显然这样的用户体验是不友好的,如果可以给用户一个按钮实现一 ...

  3. android之截屏(包括截取scrollview与listview的)

    public class ScreenShot { // 获取指定Activity的截屏,保存到png文件 public static Bitmap takeScreenShot(Activity a ...

  4. iOS当前屏幕截屏

    需求描述: 有两个ViewController 我们记做 A.B ,其中B controller只是显示下半部分: 如下图效果: 实现这种的方案很多,可以用添加View方法,  也可以用UIWindo ...

  5. IOS 上架要求视频及屏幕截屏

    客户提供上架的资料 1.IOS 上架要求视频演示,录制一段视频,上传到优酷,需要url连接. 2.手机截屏,每个尺寸5张.5s/6/6p *5=15张.截屏图片分辨率. iPhone4s手机 3.5I ...

  6. delphi 屏幕截屏

    function GetScreenAll: TBitmap; // 截取全屏 var C: TCanvas; begin C := TCanvas.Create; result := TBitmap ...

  7. C# 实现屏幕截屏

    //屏幕宽            int iWidth = Screen.PrimaryScreen.Bounds.Width;            //屏幕高            int iHe ...

  8. [Xcode 实际操作]九、实用进阶-(15)屏幕截屏:截取当前屏幕上的显示内容

    目录:[Swift]Xcode实际操作 本文将演示如何截取屏幕画面,并将截取图片,存入系统相册. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UI ...

  9. iOS屏幕截屏

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

随机推荐

  1. ntoskrnl符号在IDA中查看的问题

    最近发现x64的ntoskrnl.exe,如果直接在IDA中查看,会有一些函数IDA没有识别出来,比如

  2. 【翻译】Ext JS 5:为不同设备设置不同的主题

    原文:Sencha Ext JS 5: Supporting Different Themes for Different Devices 步骤1创建一个应用程序 步骤2定义主题 步骤3编辑Appjs ...

  3. noSQL数据库相关软件介绍(大数据存储时候,必须使用)

    目前图数据库软件七种较为流行:Neo4J, Infinite Graph, DEX,InfoGrid, HyperGraphDB, Trinity, AllegroGraph(http://tech. ...

  4. javascript之DOM编程正则表达式引入

    在javascript中,正则表达式和java中区别不大.只有一小部分不同的地方: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Trans ...

  5. Android必知必会--NinePatch图片制作

    本文为CSDN学院免费课程<NinePatch图片制作从入门到精通>的笔记,建议新手先观看视频,整理此笔记是为了便于自己复习,有NinePatch基础的朋友可以直接观看第四部分.--[转载 ...

  6. 【Linux学习笔记】关于ubuntu开机菜单栏和任务栏不见了的有效解决方法

    (一) 问题描述 ubuntu开机只有桌面,没有菜单栏和任务栏,如下图: (二) 问题解决 刚学习ubuntu,总有些像我这样不折腾就不舒服的人,今天改了一下主题,图标什么的,重启开机后就发现!咦!我 ...

  7. Java集合之TreeSet

    TreeSet是一个有序的集合,它的作用是提供有序的Set集合.它继承了AbstractSet抽象类,实现了NavigableSet<E>,Cloneable,Serializable接口 ...

  8. AngularJS进阶(五)Angular实现下拉菜单多选

    Angular实现下拉菜单多选 写这篇文章时,引用文章地址如下: http://ngmodules.org/modules/angularjs-dropdown-multiselect http:// ...

  9. VS2010中使用Jquery调用Wcf服务读取数据库记录

    VS2010中使用Jquery调用Wcf服务读取数据库记录 开发环境:Window Servere 2008 +SQL SERVE 2008 R2+ IIS7 +VS2010+Jquery1.3.2 ...

  10. OpenCV——色调映射

    // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...