Swift - UIView的无损截图

效果

源码
//
// UIView+ScreensShot.swift
// Swift-Animations
//
// Created by YouXianMing on 16/9/5.
// Copyright © 2016年 YouXianMing. All rights reserved.
// import UIKit extension UIView { /**
Get the view's screen shot, this function may be called from any thread of your app. - returns: The screen shot's image.
*/
func screenShot() -> UIImage? { guard frame.size.height > && frame.size.width > else { return nil
} UIGraphicsBeginImageContextWithOptions(frame.size, false, )
layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext() return image
}
}
使用
//
// ViewController.swift
// ScreenShot
//
// Created by YouXianMing on 16/9/5.
// Copyright © 2016年 YouXianMing. All rights reserved.
// import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let redView = UIView(frame: CGRectMake(, , , ))
redView.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.5)
view.addSubview(redView) let imageView = UIImageView(image: redView.screenShot())
view.addSubview(imageView)
}
}
Swift - UIView的无损截图的更多相关文章
- UIView的无损截图
UIView的无损截图 说明 1. 烂大街的代码 2. 写成category后,方便直接从drawRect中获取绘制出来的图片 3. 可以直接绘制图片供按钮设置背景图片用 4. 无损截图(包括alph ...
- Swift UIView 层次调整
Swift 中添加的UIView都是有层级的. 我们先添加三个看一看 let view1=UIView(frame: CGRectMake(10, 50, 200, 200)) let view2=U ...
- Swift - UIView,UItableView,Cell设置边框方法
// 设置边框的宽度 cell.layer.borderWidth = 1 // 设置边框的颜色 cell.layer.borderColor = UIColor.blackColor().CGCol ...
- swift uiview弹出动画
UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.0, ...
- Swift - UIView的常用属性和常用方法总结
1,UIView常用的一些属性如下: frame:相对父视图的坐标和大小(x,y,w,h) bounds:相对自身的坐标和大小,所以bounds的x和y永远为0(0,0,w,h) center:相对父 ...
- swift UIview上添加视频播放
1. /// 是否显示过广告 private lazy var isLoadAV = false /// 15秒宣传视频 private var play: AVPlayer? /// 宣传视频背景 ...
- swift - 3D 视图,截图,关键字搜索
1.xib 上的 3D效果 按钮 2. import UIKit //1.导入框架 import MapKit class ViewController: UIViewController { @IB ...
- swift - UIView 设置背景色和背景图片
代码如下: let page = UIView() page.frame = self.view.bounds //直接设置颜色 page.backgroundColor = UIColor.gree ...
- Swift之基础知识
Swift之基础知识 出于对Swift3.0的学习,写下这篇基本语法的笔记.希望能帮助记忆 -0- 这边提供Swift3.0中文教材,资源链接: https://pan.baidu.com/s/1c2 ...
随机推荐
- Codeforces 723C. Polycarp at the Radio 模拟
C. Polycarp at the Radio time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...
- Cacti的基本使用
对于Cacti是通过snmpget来获取数据,使用 RRDtool绘画图形,用snmp服务获取数据,然后用rrdtool储存和更新数据,那么就可以简单理解为Cacti就是RRDTool的一个web图形 ...
- Nodejs编码转化问题
目前Node.js仅支持hex.utf8.ascii.binary.base64.ucs2几种编码的转换.对于GBK,GB2312等编码,Nodejs自带的toString()方法不支持,因此中文转化 ...
- HTTP请求响应对照表
1)请求(客户端->服务端[request]) GET(请求的方式) /books/java.html(请求的目标资源) HTTP/1.1(请求采用的协议和版本号) Accept: */*(客户 ...
- Xcode安装的推送证书所在目录
/Users/用户名/资源库/MobileDevice/Provisioning Profiles
- MySQL大小写敏感说明
Linux环境下,不是windows平台下.区别很大.注意. 一图胜千言 mysql> show create table Ac; +-------+-------------------- ...
- C# 正则表达式测试工具与分享窗体自适应类
放假刚回来,自己打算写一个正则表达式的测试工具,因为上次在网上用的一个在线正则表示测试工具就 没有很好的服务自己的,所以才有了现在的想法,想写一个C#开发者用的正则表达式测试工具!期间由于最大化时控件 ...
- static, readonly, const
static Use the static modifier to declare a static member, which belongs to the type itself rather t ...
- 融云官方cordova示例使用指南
最近公司要在App里加IM功能,用融云IM.于是下载其cordova版示例:https://github.com/rongcloud/cordova-plugin-rongcloud-im-demo ...
- OkHttp使用进阶 译自OkHttp Github官方教程
版权声明: 欢迎转载,但请保留文章原始出处 作者:GavinCT 出处:http://www.cnblogs.com/ct2011/p/3997368.html 没有使用过OkHttp的,可以先看Ok ...