UIView的无损截图
UIView的无损截图


说明
1. 烂大街的代码
2. 写成category后,方便直接从drawRect中获取绘制出来的图片
3. 可以直接绘制图片供按钮设置背景图片用
4. 无损截图(包括alpha通道值也被无损保存)
源码
//
// UIView+ScreensShot.h
// ColorfulView
//
// Created by YouXianMing on 15/7/17.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @interface UIView (ScreensShot) /**
* 无损截图
*
* This function may be called from any thread of your app.
*
* @return 返回生成的图片
*/
- (UIImage *)screenShot; @end
//
// UIView+ScreensShot.m
// ColorfulView
//
// Created by YouXianMing on 15/7/17.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "UIView+ScreensShot.h"
#import <objc/runtime.h> @implementation UIView (ScreensShot) - (UIImage *)screenShot { if (self && self.frame.size.height && self.frame.size.width) { UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, );
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); return image; } else { return nil;
} } @end
//
// ViewController.m
// ColorfulView
//
// Created by YouXianMing on 15/7/10.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "UIView+ScreensShot.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; UIView *cyanView = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
cyanView.backgroundColor = [UIColor cyanColor];
cyanView.alpha = 0.5f;
[self.view addSubview:cyanView]; UIImageView *imageView = [[UIImageView alloc] initWithImage:[cyanView screenShot]];
imageView.frame = CGRectMake(, , , );
[self.view addSubview:imageView];
} @end
UIView的无损截图的更多相关文章
- Swift - UIView的无损截图
Swift - UIView的无损截图 效果 源码 // // UIView+ScreensShot.swift // Swift-Animations // // Created by YouXia ...
- 【iOS】控件截图、MP4格式视频流和m3u8格式视频流截取某一帧功能的实现
最近开发遇到一个点击按钮实现直播视频流截屏的功能,去网上查了一下资料,总结了一下iOS中截屏相关的知识,然后自己做了个demo. demo主要实现了3种截屏方法,分别对应三种不同的应用场景. 1.im ...
- [翻译] MotionBlur
MotionBlur https://github.com/fastred/MotionBlur MotionBlur allows you to add motion blur effect to ...
- iOS 关于监听手机截图,UIView生成UIImage, UIImage裁剪与压缩的总结
一. 关于监听手机截图 1. 背景: 发现商品的售价页总是被人转发截图,为了方便用户添加截图分享的小功能 首先要注册用户截屏操作的通知 - (void)viewDidLoad { [super vi ...
- UIScrollView,UIView转换UIImage代码(整个view截图, 不只是可视区域)
-(UIImage*)captureView:(UIView *)theView{ CGRect rect = theView.frame; if ([theView isKindOf ...
- iOS UIView 动画浅谈
UIView 等会效果简单实现,哪一个登录页面的demo来举例子吧. + (void)animateWithDuration:(NSTimeInterval)duration animations:( ...
- 【Android测试】Android截图的深水区
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/6113059.html 需求 这两天遇到这样一个事情,因为某 ...
- Quartz2D之生成圆形头像、打水印、截图三种方法的封装
我给UIImage类添加了一个类目,用于封装三个方法,每个方法都没有难度,做这个主要为了练习一下封装: 首先在类目.h文件中声明三个方法:以及创建了一个枚举.用于水印方法中设定水印位置:方法说明和参数 ...
- IOS UIView 04- 自定义控件
注:本人是翻译过来,并且加上本人的一点见解. 前言 本文将讨论一些自定义视图.控件的诀窍和技巧.我们先概述一下 UIKit 向我们提供的控件,并介绍一些渲染技巧.随后我们会深入到视图和其所有者之间的通 ...
随机推荐
- solr 7.6 安装部署与遇到的问题
目录 安装 solr 配置solr 到tomcat(关键) 配置依赖包 创建tomcat solr 的 classes 文件 创建 solr 的core 的主目录(也就是存放core的位置) 修改配置 ...
- java将list转为树形结构的方法
目录 1.通过转化成json封装数据 2.通过java8 stream转换 1.通过转化成json封装数据 原始数据如下 [ { "name":"甘肃省", & ...
- linux下c的网络编程---转载
1.tcp协议
- 数据库初识--从MySQL 出发
要学Web 开发,也得先对数据库有所了解呀.数据库分门别类,多种多样,目前我选择了 MySQL . 看了 MySQL,觉得数据库处理也不是很难,主要就是一些对数据的处理,MySQL 主要就是一些命令的 ...
- thinkphp 页面静态化
页面静态化(代码在最后) 静态页面 是网页的代码都在页面中,不需要执行asp,php,jsp,.net等程序生成客户端网页代码的网页,静态页面网址中一般不含“?”.“=”.“&”等特殊符号. ...
- webapi找到了与该请求匹配的多个操作
关于这个问题是路由在找方法的时候层没有指定对,同一个链接,同样的参数导致路由找不到方法导致的报错,可以在WebApiConfig中多配置一层,比如"api/{controller}/{act ...
- DEV控件ASPxTextBox设置ClientEnabled="false"之后出现的问题
DEV控件ASPxTextBox设置ClientEnabled="false"之后,js中设置文本框的值后,按钮后台点击事件中获取文本框的值为空.
- 设计模式学习--面向对象的5条设计原则之Liskov替换原则--LSP
一.LSP简介(LSP--Liskov Substitution Principle): 定义:如果对于类型S的每一个对象o1,都有一个类型T的对象o2,使对于任意用类型T定义的程序P,将o2替换为o ...
- [源码] 定义String s="abcd", 求长度
一般会答: s.length() 看源码是如何实现的: /** * Returns the length of this string. * The length is equal to the nu ...
- Spring系列之——spring security
1 搭建springboot 2 配置pom依赖(springboot版本为2.1.3) <dependency> <groupId>org.springframework.b ...