简体   繁体   English

Swift:UIImage的大小是原来的两倍

[英]Swift: UIImage has twice the size

When I draw a border around a UIImage with 当我在UIImage周围绘制边框时

    let opaque = false
    let scale: CGFloat = 0
    UIGraphicsBeginImageContextWithOptions(CGSize.init(width: ((self.imageView?.image?.size.width)!+512), height: (self.imageView?.image?.size.height)!+512), opaque, scale)
    let context = UIGraphicsGetCurrentContext()

    context?.setFillColor(UIColor.yellow.cgColor)
    context?.fill(CGRect.init(x: 0, y: 0, width: (context?.width)!, height: (context?.width)!))

    self.imageView?.image?.draw(at: CGPoint.init(x: 256, y: 256))

    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

and print out its size, it is totally correct, as I set it: width and height each + 256. 并打印出它的大小,这是完全正确的,正如我所设定的:宽度和高度各为256。

However, when I save the image to library, it has twice its width and twice its height. 但是,当我将图像保存到库时,它的宽度是其两倍,高度是其两倍。 I have no idea why. 我不知道为什么。 Also (and this is the part that annoys me the most), when I want to draw something on the image, also it seem to have twice the sizes... 另外(这是最让我烦恼的部分),当我想在图像上绘制某些东西时,它似乎也有两倍的尺寸...

Thanks, Maddy, for bringing me on the right track! 谢谢,玛迪,让我走上了正轨!

The solution is: The ScaleFactor has to be 1: 解决方案是:ScaleFactor必须为1:

let scale: CGFloat = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM