gpt4 book ai didi

ios - 将图像添加到 CAShapeLayer Swift 3

转载 作者:行者123 更新时间:2023-12-02 04:16:28 30 4
gpt4 key购买 nike

我正在尝试绘制一个带有段数组和每个切片中心的图像的饼图,我已经绘制了饼图,但无法将图像添加到cashapelayer。我引用了以下帖子Swift: Add image to CAShapeLayer ,但这并没有解决我的问题。

这是我尝试过的。

override func draw(_ rect: CGRect) {
//let context = UIGraphicsGetCurrentContext()
let radius = min(frame.size.width,frame.size.height) * 0.5;
let viewCenter = CGPoint(x: frame.size.width * 0.5, y: frame.size.height * 0.5)
let valueCount = segments.reduce(0){ $0 + $1.value}
var startAngle = -CGFloat(M_PI / 2)

for segment in segments {
let endAngle = startAngle + CGFloat(M_PI * 2) * (segment.value)/valueCount

/*context?.move(to: CGPoint(x: viewCenter.x, y: viewCenter.y))
context?.setFillColor(segment.color.cgColor)
context?.setStrokeColor(UIColor.black.cgColor)
context?.setLineWidth(3.0)
context?.addArc(center: viewCenter, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: false)
context?.fillPath()*/

let pieSliceLayer = CAShapeLayer()
let slicePath = UIBezierPath(arcCenter: viewCenter, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: false)
pieSliceLayer.path = slicePath.cgPath
pieSliceLayer.fillColor = segment.color.cgColor
self.layer.addSublayer(pieSliceLayer)

let halfAngle = startAngle + (endAngle - startAngle) * 0.5;
let imagePositionValue = CGFloat(0.67)
let segmentCenter = CGPoint(x:viewCenter.x+radius*imagePositionValue*cos(halfAngle),y:viewCenter.y+radius*imagePositionValue*sin(halfAngle))

//If image is associated , add the image to the section
let imageLayer = CAShapeLayer()
let image = UIImage(named: segment.imageName)
imageLayer.contents = image?.ciImage
imageLayer.frame = CGRect(x: segmentCenter.x - 20, y: segmentCenter.y - 20, width: 20, height: 20)
self.layer.addSublayer(imageLayer)
startAngle = endAngle

}
}

我在 Objective-C 中尝试了类似的方法,效果很好,

CALayer *imageLayer = (CALayer *)[[pieLayer sublayers] objectAtIndex:0];
//Adding image
CALayer *layer = [CALayer layer];
layer.backgroundColor = [[UIColor clearColor] CGColor];
layer.bounds = CGRectMake(imageLayer.bounds.origin.x +20 ,
imageLayer.bounds.origin.y + 20, 15, 15);
NSString *imageName = [NSString stringWithString:[[self.goalSettingsModel objectAtIndex:value] valueForKeyPath:@"imageName"]];
layer.contents = (id)[UIImage imageNamed:imageName].CGImage;
[imageLayer addSublayer:layer];
[imageLayer setNeedsDisplay];

我不确定我是否错过了 calayer 的任何设置。

最佳答案

设置图层的位置属性后,图像已正确添加到形状图层中,

let imageLayer = CALayer()
imageLayer.backgroundColor = UIColor.clear.cgColor
imageLayer.bounds = CGRect(x: centerPoint.x, y: centerPoint.y , width: 15, height: 15)
imageLayer.position = CGPoint(x:centerPoint.x ,y:centerPoint.y)
imageLayer.contents = UIImage(named:segment.imageName)?.cgImage
self.layer.addSublayer(pieSliceLayer)
self.layer.addSublayer(imageLayer)

关于ios - 将图像添加到 CAShapeLayer Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40165802/

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