Swift: UIGraphicsBeginImageContextWithOptions set to 0, but not applicable

I used to resize the image using the following code, and it worked great with scale factor. Now with Swift 3 I can’t understand why the scale factor is not taken into account. The image changes, but the scale factor is not applied. You know why?

let layer = self.imageview.layer

UIGraphicsBeginImageContextWithOptions(layer.bounds.size, true, 0)

layer.render(in: UIGraphicsGetCurrentContext()!)
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

print("SCALED IMAGE SIZE IS \(scaledImage!.size)")
print(scaledImage!.scale)

For example, if I take a screenshot on the iPhone 5, the image size will be 320 * 568. I used 640 * 1136 with exactly the same code. What can lead to the fact that the scale factor will not be applied?

When I print the image scale, it will print 1, 2 or 3 based on the resolution of the device, but will not be applied to the image taken from the context.

+4
1

scaledImage!.size .

CGImageGetWidth CGImageGetHeight ( ) image.size * image.scale

, import CoreGraphics

let imageSize = scaledImage!.size //(320,568)
let imageWidthInPixel = CGImageGetWidth(scaledImage as! CGImage) //640
let imageHeightInPixel = CGImageGetHeight(scaledImage as! CGImage) //1136
+3

Source: https://habr.com/ru/post/1655683/


All Articles