drawing UIImage inside a CGMutablePath

夙愿已清 提交于 2020-01-21 15:24:08

问题


Given a CGMutablePathRef how do I draw a UIImage in it? I know that you can draw it like the following:

 UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        [scaledImage drawAtPoint:CGPointMake(0, 0)];
        CGContextRestoreGState(context);

but I am not sure if I have a path (which is a rectangle with rounded corner) how do I add an image in it?


回答1:


When you say "draw within a CGMutablePath" I have to assume you mean "clip to a CGMutablePath." In that case, you just need to add the path to the current context (CGContextAddPath() or any of the path-building functions like CGContextAddLineToPoint()) and then call CGContextClip(). You can then draw your image in the context with something like drawAtPoint: or CGContextDrawImage().




回答2:


I think that you are choosing the hard way of doing it.

Your approach assumes that you have already got a bitmap and created a bitmap based graphics context. At your place I would just use UIImage's :

- (void)drawAtPoint:(CGPoint)point;


来源:https://stackoverflow.com/questions/14249304/drawing-uiimage-inside-a-cgmutablepath

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