【发布时间】:2021-03-01 22:04:27
【问题描述】:
我正在使用单元测试在我的 Swift 项目中测试图像的旋转。
当我使用 iOS 13.7 测试和使用 iOS 11.2 测试时,我得到的结果与 pngData() 不同。
也很奇怪,我认为这是相关的,而且我真正的问题是 -
在 iOS 13.7 上比较 2 个图像 - 静态图像和旋转图像 - 返回它们的数据大小相同。
在 iOS 11.2 上 - 我的静态图像将其数据大小更改了 X AMOUNT,我的旋转图像将其数据大小更改了 Y AMOUNT,现在它们具有不同的数据大小,我的测试失败了。
旋转函数 -
func cld_rotate(_ degree: Float) -> UIImage? {
var newSize = CGRect(origin: CGPoint.zero, size: self.size).applying(CGAffineTransform(rotationAngle: cld_radians(from: Double(degree)))).size
// Trim off the extremely small float value to prevent core graphics from rounding it up
newSize.width = floor(newSize.width)
newSize.height = floor(newSize.height)
UIGraphicsBeginImageContextWithOptions(newSize, false, self.scale)
let context = UIGraphicsGetCurrentContext()!
// Move origin to middle
context.translateBy(x: newSize.width/2, y: newSize.height/2)
// Rotate around middle
context.rotate(by: cld_radians(from: Double(degree)))
// Draw the image at its center
draw(in: CGRect(x: -self.size.width/2, y: -self.size.height/2, width: self.size.width, height: self.size.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
知道为什么pngData() 在两个不同的 iOS 中使用的不一样吗?为什么旋转后的图像会改变其数据大小而不是静态图像?
【问题讨论】:
标签: ios swift unit-testing rotation uiimage