我正在尝试裁剪此图片,这是一个SKSpriteNode:
我正在尝试从顶部裁剪此图像,以便保持此形状的底部半圆。例如,它会被裁剪为:
所以我使用这两种方法来完成这项任务:
func recalculateScore() {
currentScore -= decreaseRate
let image = UIImage(cgImage: (vial.texture?.cgImage())!)
vial.texture = SKTexture(image: cropBottomImage(image: image))
}
func cropBottomImage(image: UIImage) -> UIImage {
let height = CGFloat(image.size.height / 3)
let rect = CGRect(x: 0, y: image.size.height - height, width: image.size.width, height: height)
return cropImage(image: image, toRect: rect)
}
func cropImage(image:UIImage, toRect rect:CGRect) -> UIImage {
let imageRef:CGImage = image.cgImage!.cropping(to: rect)!
let croppedImage:UIImage = UIImage(cgImage:imageRef)
return croppedImage
}
然而,这导致了这个结果:
好像它正在被压缩。我想我的问题可能就在这一行: let rect = CGRect(x:0,y:image.size.height - height,width:image.size.width,height:height)
(0,0)的CGRect坐标是否位于最左上角?我对CGRect的x和y参数意味着什么感到困惑?
答案 0 :(得分:0)
调整精灵的大小,正在发生的事情是裁剪的纹理正在拉伸以填充精灵,并且由于你只是垂直裁剪,它只会垂直拉伸
func recalculateScore() {
currentScore -= decreaseRate
let image = UIImage(cgImage: (vial.texture?.cgImage())!)
vial.texture = SKTexture(image: cropBottomImage(image: image))
vial.size = vial.texture.size
}