UIGraphicsBeginImageContext

UIGraphicsBeginImageContext

首先,先来认识一个UIGraphicsBeginImageContext,它会创建一个基于位图的上下文(context)(默认创建一个透明的位图上下文),并将其设置为当前上下文。

位图图形上下文UIKit是不会负责创建的,所以需要用户手动创建,并且需要在使用完毕后关闭它。在使用UIKit中系统创建的图形上下文的时候,我们只能在drawRect:方法中使用,由于位图图形上下文是由我们手动创建的,所以可以放到任何方法中调用,此外,这个方法并没有返回值,如果我们要得到我们创建的图形上下文只需要在创建上下文之后、关闭之前调用UIGraphicsGetCurrentContext()方法,此时取得的上下文就是我们自己创建的图形上下文了。

方法声明如下:

void UIGraphicsBeginImageContext(CGSize size);

参数size为新创建的位图上下文的大小。它同时是由UIGraphicsGetImageFromCurrentImageContext函数返回的图形的大小。该函数的功能通UIGraphicsBeginImageContextWithOptions的功能相同,相当于UIGraphicsBeginImageContextWithOptions的opaque的参数为NO,scale因子为1.0.

方法声明如下:

void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);

size:同UIGraphicsBeginImageContext

opaque:透明开关,如果图形完全不同透明,设置为YES以优化位图的存储。

scale:缩放因子。 


demo1:根据颜色生成一张图片

static func colorImage( size : CGSize, color : UIColor ) -> UIImage {
        
        //  开启一个图形上下文
        UIGraphicsBeginImageContext(size)
        
        //  获取到这个上下文
        let context = UIGraphicsGetCurrentContext()
        
        //  设置颜色
        context?.setFillColor(color.cgColor)
        
        //  绘制
        context?.fill(CGRect(x: 0, y: 0,  size.width, height: size.height))
        
        //  获取到这张图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        //  关闭
        UIGraphicsEndImageContext()
        
        return image!
    }

demo2:获取屏幕截图

//  获取屏幕的截图
    static func screenImage() -> UIImage {
        
        //  获取到window
        let window = UIApplication.shared.delegate?.window as? UIWindow
        
        //  开启一个图形上下文
        UIGraphicsBeginImageContext(UIScreen.main.bounds.size)

        //  系统截屏方法
        window?.drawHierarchy(in: UIScreen.main.bounds, afterScreenUpdates: true)
        
        //  获取到这张图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        //  关闭
        UIGraphicsEndImageContext()

        return image!
        
    }

demo3:根据view生成图片

//  根据view生成图片
    static func viewImage(view : UIView) -> UIImage {
        
        //  开启一个图形上下文
        UIGraphicsBeginImageContext(view.frame.size)
        
        //  获取到这个上下文
        let context = UIGraphicsGetCurrentContext()
        
     // 渲染内容到上下文
        view.layer.render(in: context!)
        
        //  获取到这张图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        //  关闭
        UIGraphicsEndImageContext()
        
        return image!

    }

demo4:直接将图片切割圆角

//  切割图片生成圆角
    func cicleImage() -> UIImage {
        //  开启一个图形上下文
        UIGraphicsBeginImageContext(size)
        
        //  获取到这个上下文
        let context = UIGraphicsGetCurrentContext()
        
        //  设置圆形
        context?.addEllipse(in: CGRect(x: 0, y: 0,  self.size.width, height: self.size.height))
        
        //  裁剪
        context?.clip()
        
        //  重新绘制
        self.draw(in: CGRect(x: 0, y: 0,  self.size.width, height: self.size.height))
        
        //  获取到这张图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        //  关闭
        UIGraphicsEndImageContext()
        
        return image!

    }

  

原文地址:https://www.cnblogs.com/chenjiangxiaoyu/p/7464569.html