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, 0);
        [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(30, 30, 100, 100)];
    cyanView.backgroundColor = [UIColor cyanColor];
    cyanView.alpha           = 0.5f;
    [self.view addSubview:cyanView];

    UIImageView *imageView   = [[UIImageView alloc] initWithImage:[cyanView screenShot]];
    imageView.frame          = CGRectMake(80, 80, 100, 100);
    [self.view addSubview:imageView];
}



@end



目录
相关文章
|
小程序 前端开发
微信小程序中使用画布canvas实现动态心电图绘制
微信小程序中使用画布canvas实现动态心电图绘制
618 0
微信小程序中使用画布canvas实现动态心电图绘制
|
小程序 定位技术
微信小程序:map地图自动缩放自适应大小
微信小程序:map地图自动缩放自适应大小
966 0
|
3月前
|
小程序
【亲测有效】支持横竖屏 微信小程序video禁止进度条拖动,微信小程序遮罩进度条,
【亲测有效】支持横竖屏 微信小程序video禁止进度条拖动,微信小程序遮罩进度条,
74 1
【亲测有效】支持横竖屏 微信小程序video禁止进度条拖动,微信小程序遮罩进度条,
|
C++
Qt图片定时滚动播放器+透明过渡动画
解决:[QWidget::paintEngine: Should no longer be called QPainter::begin: Paint device returned engine == 0, type: 1] 需要在哪个控件上绘制,就要在哪个控件类中重写 paintEvent() ,所以本项目 需要使用自定义的MyQLabel继承QLabel
133 0
|
5月前
|
前端开发 小程序
微信小程序canvas画布绘制;canvas画布图片保存
微信小程序canvas画布绘制;canvas画布图片保存
82 0
[笔记]音视频学习之SDL篇《十一》图片 缩放 旋转
[笔记]音视频学习之SDL篇《十一》图片 缩放 旋转
142 0
|
编解码 前端开发 小程序
微信小程序canvas画布不清晰解决方法
微信小程序canvas画布不清晰解决方法
|
小程序 容器
【微信小程序】image组件的4种缩放模式与9种裁剪模式
假设有一个容器(这个容器的宽高就是设置的样式),要将图片放进去。而aspectFit的特点就是保持图片不变形,且容器要“刚好”将这个图片装进去。如果原始图片比容器大,就要被等比例缩小;如果原始图片比容器小,就会被等比例放大。一直放大或缩小到图片的某一条边刚好和容器的一条边重合。
1332 0
从一个圆环进度功能来学习SVG
从一个圆环进度功能来学习SVG
145 0
从一个圆环进度功能来学习SVG
|
iOS开发
iOS动画开发之三——UIView的转场切换
iOS动画开发之三——UIView的转场切换
372 0