Is it possible to add a background Image to a CAShape? (iPhone)

烂漫一生 提交于 2019-12-24 19:11:21

问题


I am trying to distort an image in an arbitrary way, following for example a bezier shape. I have used CAShapeLayer to create the shape, but then it seems that setting its contents to an Image does not work. How can I have the image distorted following that shape? Is it even possible?

Thank you!


回答1:


This is currently not possible. I would suggest filing a radar (http://bugreport.apple.com) if you think this should be supported.

See also: Can a CGImage be added to the contents property of a CAShapeLayer?




回答2:


it is not possible to do so, but i succeeded in doing so by putting a CALayer on top a CAShapeLayer. this is my code:

CALayer *image =[CALayer layer];
image.frame = CGRectMake(0, 0, 75, 75);
image.contents = (id) [UIImage imageNamed:@"number-1"].CGImage;    
 [self.layer addSublayer:image];



UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 75, 75)];
blueCircleLayer = [CAShapeLayer layer];
blueCircleLayer.path = circle.CGPath;
blueCircleLayer.strokeColor = [UIColor blueColor].CGColor;
blueCircleLayer.fillColor = [UIColor clearColor].CGColor;
blueCircleLayer.shadowColor =[UIColor blackColor].CGColor;
blueCircleLayer.shadowOffset = CGSizeMake(0.0f, 5.0f);
blueCircleLayer.shadowOpacity = 0.7f;
blueCircleLayer.lineWidth = 7.0;

[self.layer addSublayer:blueCircleLayer];


来源:https://stackoverflow.com/questions/5578801/is-it-possible-to-add-a-background-image-to-a-cashape-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!