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

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

    iOS Swift

    Using modern UIGraphicsImageRenderer

    public extension UIView {
        @available(iOS 10.0, *)
        public func renderToImage(afterScreenUpdates: Bool = false) -> UIImage {
            let rendererFormat = UIGraphicsImageRendererFormat.default()
            rendererFormat.opaque = isOpaque
            let renderer = UIGraphicsImageRenderer(size: bounds.size, format: rendererFormat)
    
            let snapshotImage = renderer.image { _ in
                drawHierarchy(in: bounds, afterScreenUpdates: afterScreenUpdates)
            }
            return snapshotImage
        }
    }
    

提交回复
热议问题