How to draw a stroke outline using CGContext API?
I have an icon image I would like to draw a 1px, colored 0.5 opacity line around, inside the image (similar to Stroke layer style with position:inside in Photoshop). So far I've tried this:
UIGraphicsBeginImageContextWithOptions(inCompositeSize, NO, inBaseImage.scale);
context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, frame, image.CGImage);
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
CGContextSetStrokeColorSpace(context, cs);
CGColorSpaceRelease(cs);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextStrokePath(context);
//...
This seems to have no effect. 开发者_JS百科I'm guessing this is because I'm working with an image and not a path. How can I produce an outline stroke effect on the image?
Clarification: I'm trying to draw an outline on the opaque icon area only. The icon background is 0 alpha.
StrokePath
strokes a path, and you haven't plotted a path. That's why it strokes nothing.
You need to do either of two things:
- Vectorize the raster image and stroke that path. Core Image's edge-detection filter will help, but even so, this is hard. There is no built-in API for this; you'll have to find a library that does it or do it yourself.
- Fake it by drawing the image with a shadow. Note that shadows can be any color; it doesn't have to look like a shadow.
精彩评论