Ios CoreMageContext';s CreateCGImage生成错误的CGRect

Ios CoreMageContext';s CreateCGImage生成错误的CGRect,ios,swift,skspritenode,core-image,skshapenode,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 coreImag

代码:

调用初始值设定项:

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)
    }
}
通过
width/height:102.69999494824219
,生成
shapeTexture
width/height:103
。 似乎是
coreImageContext.createCGImage
102.69999494824219
四舍五入到
103.0

这会导致较小的意外输出。我怎样才能绕过这个舍入?或者是否有其他方法为节点生成梯度图像

更多代码:

// 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)

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


相反,视图的
是以点为单位给出的,需要将点乘以视图的
contentScaleFactor
,以获得实际的像素大小(用于生成渐变)。UIKit还允许使用亚像素帧大小,但在引擎盖下,在将视图渲染到屏幕上时,它也会将视图四舍五入。

与您的问题无关,但为什么不简单地
frame.size
?@LeoDabus-对不起,我没听清楚,一个接一个地调用两个
init
s不是一个好主意。您应该只需要在那里调用self.init(circleOfRadius:radius)。您是对的,但这是一种奇怪的弥补方法。您可以尝试删除该init,但会抛出一个错误。看到一些类似的帖子,于是就实现了。有没有办法为我的渐变图像获得准确的大小?嗯,图像需要有谨慎的(整数)像素大小,没有半像素。我认为更多的是画面有点偏离了。你是怎么得到这些奇怪的数字的?你在应用仿射变换吗?我添加了一些更相关的代码,这些代码可以创建具有给定半径的形状节点。我明白你的意思,如果没有解决方法,我是否应该专注于创建具有圆角矩形的节点,以避免撞到图像的圆角?什么是
self.path!。边界框
?这听起来像是一个很好的替代品,可以代替
frame
。还可以使用创建节点时使用的半径来确定渐变纹理的大小。
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)
    }
}