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 ...
随机推荐
- php中or的含义
or其实是Php中的短路或 经常看到这样的语句: $file = fopen($filename, r) or die("抱歉,无法打开: $filename"); or在这里是这 ...
- 工作中遇到的小问题: 做弹幕从数据库取出东西均匀插入marquee中,
function getFloatContent() { var method = 'GETFLOATCONTENT'; $.ajax({ url: 'api/zhenqiapi.php', data ...
- freeCodeCamp:Repeat a string repeat a string
重复一个指定的字符串 num次,如果num是一个负数则返回一个空字符串. /*思路 fo循环将字符串重复num次并组成数组 将数组组成新的字符串并返回 */ function repeat(str, ...
- CoreLocation框架的使用---地理编码
#import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewCont ...
- apache的80端口分发
打开 conf 文件夹,找到下面的 httpd.conf 更改Listen 后面的端口号为:80:默认就是80端口 去掉下面的相关注释: #LoadModule proxy_module module ...
- Linux 修改IP地址
vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE="eth0" BOOTPROTO="static" #静 ...
- Static Constructors
A static constructor is used to initialize any static data, or to perform a particular action that n ...
- Python成长笔记 - 基础篇 (四)函数
1.面向对象:类(class) 2.面向过程:过程(def) 3.函数式编程:函数(def)----python 1.函数:http://egon09.blog.51cto.com/9161406 ...
- Python成长笔记 - 基础篇 (三)python列表元组、字典、集合
本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 一.列表和元组的操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义 ...
- 用C写的俄罗斯方块游戏 By: hoodlum1980 编程论坛
/************************************ * Desc: 俄罗斯方块游戏 * By: hoodlum1980 * Email: jinfd@126.com * Dat ...