ios - CoreImageContext 的 CreateCGImage 产生了错误的 CGRect

标签 ios swift skspritenode core-image skshapenode

代码:

enum GradientDirection {
    case up
    case left
    case upLeft
    case upRight
}

extension SKTexture {
    convenience init(size: CGSize, color1: CIColor, color2: CIColor, direction: GradientDirection = .up) {
        let coreImageContext = CIContext(options: nil)
        let gradientFilter = CIFilter(name: "CILinearGradient")
        gradientFilter!.setDefaults()

        var startVector:CIVector
        var endVector:CIVector

        switch direction {
        case .up:
            startVector = CIVector(x: size.width/2, y: 0)
            endVector = CIVector(x: size.width/2, y: size.height)
        case .left:
            startVector = CIVector(x: size.width, y: size.height/2)
            endVector = CIVector(x: 0, y: size.height/2)
        case .upLeft:
            startVector = CIVector(x: size.width, y: 0)
            endVector = CIVector(x: 0, y: size.height)
        case .upRight:
            startVector = CIVector(x: 0, y: 0)
            endVector = CIVector(x: size.width, y: size.height)

        }
        gradientFilter!.setValue(startVector, forKey: "inputPoint0")
        gradientFilter!.setValue(endVector, forKey: "inputPoint1")
        gradientFilter!.setValue(color1, forKey: "inputColor0")
        gradientFilter!.setValue(color2, forKey: "inputColor1")

        let imgRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        let cgimg = coreImageContext.createCGImage(gradientFilter!.outputImage!, from: imgRect)!

        print("cgimg: ",  cgimg) // *** Observer this output ***** 103.0 width and height

        self.init(cgImage: cgimg)
    }
}

调用初始化程序:

// e.g. CGSize(width: 102.69999694824219, height: 102.69999694824219)
let textureSize = CGSize(width: self.frame.width, height: self.frame.height)
let shapeTexture = SKTexture(size: textureSize, color1: bottomColor, color2: topColor, direction: .upRight)

传递 width/height: 102.69999694824219,生成 shapeTexturewidth/height: 103。 似乎 coreImageContext.createCGImage 正在将 102.69999694824219 舍入为 103.0

这会导致轻微的意外输出。我怎样才能绕过这个舍入?或者有没有其他方法可以为节点生成渐变图像?

更多代码:

class BubbleNode: SKShapeNode {

    private var backgroundNode: SKCropNode!
    var label: SKLabelNode!

    private var state: BubbleNodeState!

    private let BubbleAnimationDuration = 0.2
    private let BubbleIconPercentualInset = 0.4

    var model: BubbleModel! {
        didSet {
            self.label.text = model.name
        }
    }

    override init() {
        super.init()
    }

    convenience init(withRadius radius: CGFloat) {
        self.init()
        self.init(circleOfRadius: radius)
        state = .normal
        self.configure()
    }

    private func configure() {
        self.name = "mybubble"

        physicsBody = SKPhysicsBody(circleOfRadius: 4 + self.path!.boundingBox.size.width / 2.0)

        physicsBody!.isDynamic = true
        physicsBody!.affectedByGravity = false
        physicsBody!.allowsRotation = false
        physicsBody!.mass = 0.3
        physicsBody!.friction = 0.0
        physicsBody!.linearDamping = 3

        backgroundNode = SKCropNode()
        backgroundNode.isUserInteractionEnabled = false
        backgroundNode.position = CGPoint.zero
        backgroundNode.zPosition = 0
        self.addChild(backgroundNode)

        label = SKLabelNode(fontNamed: "")
        label.preferredMaxLayoutWidth = self.frame.size.width - 16
        label.numberOfLines = 0
        label.position = CGPoint.zero
        label.fontColor = .white
        label.fontSize = 10
        label.isUserInteractionEnabled = false
        label.verticalAlignmentMode = .center
        label.horizontalAlignmentMode = .center
        label.zPosition = 2
        self.addChild(label)
    }

    func addGradientNode(withRadius radius: CGFloat) {
        let gradientNode = SKShapeNode(path: self.path!)
        gradientNode.zPosition = 1
        gradientNode.fillColor = .white
        gradientNode.strokeColor = .clear

        let bottomColor = CIColor(red: 0.922, green: 0.256, blue: 0.523, alpha: 1)
        let topColor = CIColor(red: 0.961, green: 0.364, blue: 0.155, alpha: 1)

        let textureSize = CGSize(width: self.frame.width, height: self.frame.height)
        let shapeTexture = SKTexture(size: textureSize, color1: bottomColor, color2: topColor, direction: .upRight)

        gradientNode.fillTexture = shapeTexture

        self.addChild(gradientNode)

        print("path: ", self.path!)
        print("textureSize: ", textureSize)
        print("shapeTexture: ", shapeTexture)
    }
}

最佳答案

任何图像/纹理都将始终具有整数大小,因为内存中没有子像素。因此像 Core Image 这样的框架总是会将给定的大小四舍五入到下一个整数值。

相比之下, View 的frame给出,需要乘以 View 的contentScaleFactor才能得到实际的像素大小(您应该使用它来生成渐变)。 UIKit 还允许亚像素帧大小,但在幕后,它还会在将 View 渲染到屏幕时四舍五入。

关于ios - CoreImageContext 的 CreateCGImage 产生了错误的 CGRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59771337/

相关文章:

ios - 如何在 UITabBar 的按钮上设置 accessibilityIdentifier

ios - Spritekit 碰撞

ios - 检测用户何时点击 SKSpriteNode

swift - 如何使 'n' 数量的 spriteNodes 出现在屏幕大小内并在屏幕内移动?

ios - 为什么我的 NSURLSessionDownloadTask 下载无法到达我的客户端?

ios - iOS CoreBluetooth Per/Centr交叉连接

ios - 使用 UserDefaults 设置全局变量

ios - 如何从第一个数组中查找第二个数组中的对象? swift iOS

ios - 第一句话有不同的颜色,然后是 2 条换行后开始的句子

ios - 在 Cordova 混合应用程序中使用 Helvetica Neue