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

前端 未结 17 1520
时光取名叫无心
时光取名叫无心 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:25

    All Swift 3 answers did not worked for me so I have translated the most accepted answer:

    extension UIImage {
        class func imageWithView(view: UIView) -> UIImage {
            UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0.0)
            view.layer.render(in: UIGraphicsGetCurrentContext()!)
            let img: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return img!
        }
    }
    

提交回复
热议问题