How to capture UIView to UIImage without loss of quality on retina display

前端 未结 17 1880
时光取名叫无心
时光取名叫无心 2020-11-22 00:42

My code works fine for normal devices but creates blurry images on retina devices.

Does anybody know a solution for my issue?

+ (UIImage *) imageWith         


        
17条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 01:23

    For Swift 5.1 you can use this extension:

    extension UIView {
    
        func asImage() -> UIImage {
            let renderer = UIGraphicsImageRenderer(bounds: bounds)
    
            return renderer.image {
                rendererContext in
    
                layer.render(in: rendererContext.cgContext)
            }
        }
    }
    

提交回复
热议问题