如何从UIView的内容获取CGImageRef? [英] How to obtain a CGImageRef from the content of an UIView?

查看:200
本文介绍了如何从UIView的内容获取CGImageRef?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIView,我在其中绘制一些东西-drawRect:。现在我需要一个CGImageRef从这个图形上下文或位图的UIView。

I have an UIView where I was drawing some stuff inside -drawRect:. Now I need a CGImageRef from this graphics context or bitmap of the UIView. Is there an easy way to get that?

推荐答案

像这样(从内存中输入,所以它可能不是100%正确) :

Like this (typed from memory, so it might not be 100% correct):

// Get a UIImage from the view's contents
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, view.contentScaleFactor);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// Convert UIImage to CGImage
CGImageRef cgImage = image.CGImage;

这篇关于如何从UIView的内容获取CGImageRef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆