UIView的无损截图

说明

1. 烂大街的代码

2. 写成category后,方便直接从drawRect中获取绘制出来的图片

3. 可以直接绘制图片供按钮设置背景图片用

4. 无损截图(包括alpha通道值也被无损保存)

源码

  1. //
  2. // UIView+ScreensShot.h
  3. // ColorfulView
  4. //
  5. // Created by YouXianMing on 15/7/17.
  6. // Copyright (c) 2015年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10.  
  11. @interface UIView (ScreensShot)
  12.  
  13. /**
  14. * 无损截图
  15. *
  16. * This function may be called from any thread of your app.
  17. *
  18. * @return 返回生成的图片
  19. */
  20. - (UIImage *)screenShot;
  21.  
  22. @end
  1. //
  2. // UIView+ScreensShot.m
  3. // ColorfulView
  4. //
  5. // Created by YouXianMing on 15/7/17.
  6. // Copyright (c) 2015年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. #import "UIView+ScreensShot.h"
  10. #import <objc/runtime.h>
  11.  
  12. @implementation UIView (ScreensShot)
  13.  
  14. - (UIImage *)screenShot {
  15.  
  16. if (self && self.frame.size.height && self.frame.size.width) {
  17.  
  18. UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, );
  19. [self.layer renderInContext:UIGraphicsGetCurrentContext()];
  20. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  21. UIGraphicsEndImageContext();
  22.  
  23. return image;
  24.  
  25. } else {
  26.  
  27. return nil;
  28. }
  29.  
  30. }
  31.  
  32. @end
  1. //
  2. // ViewController.m
  3. // ColorfulView
  4. //
  5. // Created by YouXianMing on 15/7/10.
  6. // Copyright (c) 2015年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. #import "UIView+ScreensShot.h"
  11.  
  12. @interface ViewController ()
  13.  
  14. @end
  15.  
  16. @implementation ViewController
  17.  
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20.  
  21. self.view.backgroundColor = [UIColor blackColor];
  22.  
  23. UIView *cyanView = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
  24. cyanView.backgroundColor = [UIColor cyanColor];
  25. cyanView.alpha = 0.5f;
  26. [self.view addSubview:cyanView];
  27.  
  28. UIImageView *imageView = [[UIImageView alloc] initWithImage:[cyanView screenShot]];
  29. imageView.frame = CGRectMake(, , , );
  30. [self.view addSubview:imageView];
  31. }
  32.  
  33. @end

UIView的无损截图的更多相关文章

  1. Swift - UIView的无损截图

    Swift - UIView的无损截图 效果 源码 // // UIView+ScreensShot.swift // Swift-Animations // // Created by YouXia ...

  2. 【iOS】控件截图、MP4格式视频流和m3u8格式视频流截取某一帧功能的实现

    最近开发遇到一个点击按钮实现直播视频流截屏的功能,去网上查了一下资料,总结了一下iOS中截屏相关的知识,然后自己做了个demo. demo主要实现了3种截屏方法,分别对应三种不同的应用场景. 1.im ...

  3. [翻译] MotionBlur

    MotionBlur https://github.com/fastred/MotionBlur MotionBlur allows you to add motion blur effect to ...

  4. iOS 关于监听手机截图,UIView生成UIImage, UIImage裁剪与压缩的总结

    一.  关于监听手机截图 1. 背景: 发现商品的售价页总是被人转发截图,为了方便用户添加截图分享的小功能 首先要注册用户截屏操作的通知 - (void)viewDidLoad { [super vi ...

  5. UIScrollView,UIView转换UIImage代码(整个view截图, 不只是可视区域)

    -(UIImage*)captureView:(UIView *)theView{     CGRect rect = theView.frame;     if ([theView isKindOf ...

  6. iOS UIView 动画浅谈

    UIView 等会效果简单实现,哪一个登录页面的demo来举例子吧. + (void)animateWithDuration:(NSTimeInterval)duration animations:( ...

  7. 【Android测试】Android截图的深水区

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/6113059.html 需求 这两天遇到这样一个事情,因为某 ...

  8. Quartz2D之生成圆形头像、打水印、截图三种方法的封装

    我给UIImage类添加了一个类目,用于封装三个方法,每个方法都没有难度,做这个主要为了练习一下封装: 首先在类目.h文件中声明三个方法:以及创建了一个枚举.用于水印方法中设定水印位置:方法说明和参数 ...

  9. IOS UIView 04- 自定义控件

    注:本人是翻译过来,并且加上本人的一点见解. 前言 本文将讨论一些自定义视图.控件的诀窍和技巧.我们先概述一下 UIKit 向我们提供的控件,并介绍一些渲染技巧.随后我们会深入到视图和其所有者之间的通 ...

随机推荐

  1. tensorflow VocabularyProcessor

    from tensorflow.contrib import learn import numpy as np vocab_process = learn.preprocessing.Vocabula ...

  2. 什么是Java序列化?为什么序列化?序列化有哪些方式?

    先普及一下,计算机中无法识别一个基本单元[字节]来表示,必须经过“翻译”才能让计算机理解人类的语言,这个翻译过程就是[编码],通常所说的字符转换为字节.  有I/O的地方机就会涉及编码,现在几乎所有的 ...

  3. ruby文件操作

    Ruby代码 1.#读文件 2.f = File.open("myfile.txt", "r") 3.f.each_line do|line| 4.puts & ...

  4. Linux 命令 "cp" 代码实现简介

    本blog主要是模仿Linux的cp命令的功能,未实现参数,只是基础功能部分. 本文的主要目的在于练习 文件流 和 目录流 中的函数的使用. 主要功能包括两种: 源文件属性为文件,拷贝到其它文件(内容 ...

  5. cordova打包APK,SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode ...

    javascript 严格模式 第一次接触let关键字,有一个要非常非常要注意的概念就是”javascript 严格模式”,比如下述的代码运行就会报错: let hello = 'hello worl ...

  6. BG.Sqoop

    1. 下载 Sqoop,并复制到虚拟机 http://sqoop.apache.org/ 2. 安装Sqoop tar zxf sqoop-1.4.6.bin__hadoop-2.0.4-alpha. ...

  7. ASP.NET MVC 获得 view 中的HTML并将其中的内容自动转换成繁体中文。

    一.思路 1.获得 asp.net mvc 输出的 html 的字符串. 2.将拿到的 html 字符串中的简体中文转换成繁体中文. 3.输出 html. 二.实现 1.扩展 RazorView 视图 ...

  8. ASP.NET 关于GridView 表格重复列合并

    这几天做一个项目有用到表格显示数据的地方,客户要求重复的数据列需要合并,就总结了一下GridView 和 Repeater 关于重复数据合并的方法. 效果图如下 : GridView : 前台代码 : ...

  9. 【转】Dubbo声明式缓存

    缓存的应用非常广泛,为了提高数据访问的速度.Dubbo也不例外,它提供了声明式缓存,以减少用户加缓存的工作量. 一.Dubbo中缓存策略 lru 基于最近最少使用原则删除多余缓存,保持最热的数据被缓存 ...

  10. IDEA 中edit configurations加号找不到tomcat server

    前言:在本机 idea 中搭建 springMVC 项目,正准备配置 Tomcat 时,发现没有 tomcat server 选项,而我的 idea 是有这个插件的,所以解决问题的方案应该是另一个地方 ...