dev*_*ett 2 cocoa core-graphics shadow clip
我正在尝试使用核心图形绘制图像,使其具有圆角和投影.这是我的代码片段:
CGContextSetShadowWithColor(context, CGSizeMake(0, 1), 2, shadowColor);
CGContextAddPath(context, path);
CGContextClip(context);
CGContextDrawImage(context, rect, image);
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,创建圆角的剪裁也会剪切阴影.由于图像在区域中可能是透明的,因此我不能简单地在图像下绘制带阴影的圆角矩形.我想我需要先将圆角形状应用于图像,然后将生成的图像绘制到屏幕上并添加阴影.有谁知道如何做到这一点?
谢谢!
Dan*_*rpe 11
好的,假设您有一个UIView子类,它有一个实例变量image,这是一个UIImage,那么你可以像你这样做drawRect:function ...
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
CGRect _bounds = [self bounds];
CGColorRef aColor;
CGContextRef context = UIGraphicsGetCurrentContext();
// Create a path
CGRect insetRect = CGRectInset(_bounds, kBSImageButtonBorder, kBSImageButtonBorder);
CGRect offsetRect = insetRect; offsetRect.origin = CGPointZero;
UIGraphicsBeginImageContext(insetRect.size);
CGContextRef imgContext = UIGraphicsGetCurrentContext();
CGPathRef clippingPath = [UIBezierPath bezierPathWithRoundedRect:offsetRect cornerRadius:CORNER_RADIUS].CGPath;
CGContextAddPath(imgContext, clippingPath);
CGContextClip(imgContext);
// Draw the image
[image drawInRect:offsetRect];
// Get the image
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Setup the shadow
aColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.5f].CGColor;
CGContextSetShadowWithColor(context, CGSizeMake(0.0f, 2.0f), 2.0f, aColor);
// Draw the clipped image in the context
[img drawInRect:insetRect];
}
Run Code Online (Sandbox Code Playgroud)
我自己对Quartz编程有点新意,但这应该会给你的图像,以矩形为中心,减去一个边角,一个角半径,以及2.f点阴影2.f点.希望有所帮助.
| 归档时间: |
|
| 查看次数: |
5452 次 |
| 最近记录: |