How to capture high resolution image from UIView
Asked Answered
O

4

10

I hope you all are safe.

I know this question asked several times but not getting a perfect answer.

I just wanted to capture an image from UIView with high resolution the main this is that image should not be a blur.

I have tried this code

extension UIView {
    func asImage() -> UIImage {
            let renderer = UIGraphicsImageRenderer(size: self.bounds.size)
            let capturedImage = renderer.image {
                (ctx) in
                self.drawHierarchy(in: self.bounds, afterScreenUpdates: true)
            }
            return capturedImage
    }
}

Right now while I capture the image and zoom the text is blurred.

Thanks in advance

Edited I am trying to create high-resolution image from UIView. When I zoom 1 part of image the text is blurred.

Please check below image

enter image description here

Orran answered 21/5, 2020 at 19:56 Comment(3)
Did you get any solution? I have the same issue.Trish
Yes I have a solution will post here soonOrran
Will that be possible to share the solution, I am trying other ways, but it doesn't seems to work.Trish
G
11

You can set contentScaleFactor property to get clarity as described in this.

For example you can use following code,

extension UIView {
    
        func scale(by scale: CGFloat) {
             self.contentScaleFactor = scale
             for subview in self.subviews {
                 subview.scale(by: scale)
             }
         }
    
         func takeScreenshot(with scale: CGFloat? = nil) -> UIImage {
             let newScale = scale ?? UIScreen.main.scale
             self.scale(by: newScale)
             let format = UIGraphicsImageRendererFormat()
             format.scale = newScale
             let renderer = UIGraphicsImageRenderer(size: self.bounds.size, format: format)
             let image = renderer.image { rendererContext in
                 self.layer.render(in: rendererContext.cgContext)
             }
             return image
         }
}

Get high resolution image by,

let image = myView.takeScreenshot(with: 5.0)
Gwinn answered 3/5, 2021 at 4:27 Comment(2)
thanks, It's give me high resolution image while take snapshot of view which contains background image and textSuffocate
Excellent, I have been trying multiple functions but this one worked for me. Thank you.Sojourn
G
1
extension UIView {
    func takeScreenshot() -> UIImage? {
        var screenshotImage :UIImage?
        let layer = self.layer
        let scale = UIScreen.main.scale
        UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale)
        self.drawHierarchy(in: layer.bounds, afterScreenUpdates: true)
        screenshotImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return screenshotImage
    }
}

Or maybe you can scale your view (or temporary copy of view) and then take a screenshot.

Gracchus answered 21/5, 2020 at 20:5 Comment(2)
Thanks for your answer... but getting the same result.Orran
try to scale view and then take a screenshot @OrranGracchus
P
0
extension UIImage {

    convenience init(view: UIView) {

        UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0.0)
        view.drawHierarchy(in: view.bounds, afterScreenUpdates: false)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        self.init(cgImage: (image?.cgImage)!)

    }
}

using:

let img = UIImage.init(view: self.yourView)
Pyrometallurgy answered 21/5, 2020 at 20:2 Comment(6)
Thanks for your answer. Just updated my question please check and I tried above code also.Orran
Still the same thing. I want a clear text in the image.Orran
can you share demo project to test on ?Pyrometallurgy
Just create a fresh project and place 3 to 4 labels with different sizes. Capture this and then after zoom this image. Let me know if you still need a demo then I will create it for you.Orran
no .. i will create it myself and will update you then .. thanksPyrometallurgy
@javabAli Waiting for your positive replyOrran
B
0
extension UIView {
    func takeScreenshot() -> UIImage? {
        var screenshotImage :UIImage?
        let layer = self.layer
        let scale = UIScreen.main.scale
        UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale)
        self.drawHierarchy(in: layer.bounds, afterScreenUpdates: true)
        screenshotImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return screenshotImage
    }
}

This answer would be enough, if you want a higher resolution. Just multiple scale to 5 or 10.

Bassorilievo answered 22/5, 2020 at 4:49 Comment(1)
this is the same as what told to @Alex. Please check the image in my question, I need to capture image without textblur.Orran

© 2022 - 2024 — McMap. All rights reserved.