gpt4 book ai didi

ios - Swift:UIGraphicsBeginImageContextWithOptions 比例因子设置为 0 但未应用

转载 作者:搜寻专家 更新时间:2023-10-31 08:25:26 24 4
gpt4 key购买 nike

我曾经使用以下代码调整图像大小,它过去在比例因子方面工作得很好。现在使用 Swift 3 我无法弄清楚为什么不考虑比例因子。图像已调整大小但未应用比例因子。你知道为什么吗?

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)

例如,如果我在 iPhone 5 上截屏,图像大小将为 320*568。我曾经使用完全相同的代码获得 640*1136。什么会导致不应用比例因子?

当我打印图像的比例时,它会根据设备分辨率打印 1、2 或 3,但不会应用于从上下文中获取的图像。

最佳答案

scaledImage!.size 不会返回以像素为单位的图像大小。

CGImageGetWidthCGImageGetHeight 返回相同的大小(以像素为单位)即 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

关于ios - Swift:UIGraphicsBeginImageContextWithOptions 比例因子设置为 0 但未应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39673771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com