user2029250
user2029250

Reputation: 29

Quality get reduced when convert imageview to image

In photo editor screen , I have imageview and it has background image and on top of imageview I add elements like text (label), stickers(images) etc. , Now for the final image containing all elements added on imageview , I am getting image from below code

clipRect is rect for background image inside imageview, image is aspectfit in imageview

Below is code inside UIView extension which has function to generate image out of view.

self == uiview

                  let op_format = UIGraphicsImageRendererFormat()
        op_format.scale = 1.0
        let renderer = UIGraphicsImageRenderer(bounds: CGRect(origin: clipRect!.origin, size: outputSize), format: op_format)
        let originalBound = self.bounds
        self.bounds = CGRect(origin: clipRect!.origin, size: clipRect!.size)
        var finalImage = renderer.image { ctx in
            self.drawHierarchy(in: CGRect(origin: self.bounds.origin, size: outputSize), afterScreenUpdates: true)
        }
        self.bounds = CGRect(origin: originalBound.origin, size: originalBound.size)

Issue here is quality of final image quality is very poor as compared to original background image.

Upvotes: 1

Views: 616

Answers (2)

Duncan C
Duncan C

Reputation: 131418

Don't set the scale of your UIGraphicsImageRendererFormat to 1. That forces the output image to @1x (non-retina). For most (all?) iOS devices, that will cause a 2X or 3X loss of resolution. Leave the scale value of the UIGraphicsImageRendererFormat at the default value.

Upvotes: 2

Zeeshan Ahmad II
Zeeshan Ahmad II

Reputation: 1193

you can take screenshot as well

// Convert a uiview to uiimage
    func captureView() -> UIImage {
        // Gradually increase the number for high resolution.     
        let scale = 1.0 

        UIGraphicsBeginImageContextWithOptions(bounds.size, opaque, scale)  

        layer.renderInContext(UIGraphicsGetCurrentContext()!)
        let image:UIImage  = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return image
    }

Upvotes: 0

Related Questions