2011-01-08 34 views
2

我想绘制一个图像使用核心图形,使其具有圆角和投影。这是我的代码片段:可可用圆角和阴影绘制图像

CGContextSetShadowWithColor(context, CGSizeMake(0, 1), 2, shadowColor);  
CGContextAddPath(context, path); 
CGContextClip(context); 
CGContextDrawImage(context, rect, image); 

我遇到的问题是创建圆角的剪辑也剪裁阴影。由于图像在区域中可能是透明的,因此我不能简单地在图像下绘制带有阴影的圆角矩形。我想我需要首先将圆形的形状应用到图像上,然后将生成的图像绘制到屏幕上并添加阴影。有谁知道如何做到这一点?

谢谢!

回答

11

好吧,假设你有一个UIView子类,它有一个实例变量,形象,这是一个UIImage,那么你可以做你的drawRect:函数是这样的...

- (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]; 

} 

我对Quartz编程我自己有点新,但是应该给你的图像,以矩形为中心,减去一个边界,一个圆角半径,2.f点的阴影2.f点。希望有所帮助。

0

您可以使用ImageView的一个层,该层设置阴影和边界的特性,这是多么:

self.imageView = [[NSImageView alloc] initWithFrame:NSMakeRect(0,0,60,60)]; 
    self.imageView.image = [NSImage imageNamed:@"yourImageName"]; 
    self.imageView.wantsLayer = YES; 
    self.imageView.layer.cornerRadius = 10; 
    //make the shadow and set it 
    NSShadow* shadow = [[NSShadow alloc] init]; 
    shadow.shadowBlurRadius = 2; 
    shadow.shadowOffset = NSMakeSize(2, -2); 
    shadow.shadowColor = [NSColor blackColor]; 
    self.imageView.shadow = shadow; 

希望这会有所帮助,这也是更快的绘制,然后使用的drawRect覆盖

2

以下是使用Daniel Thorpe的答案对图像四角进行圆角处理的功能,以防您像我一样来到这里,只是寻找一种方法来实现这一点。

+ (UIImage *) roundCornersOfImage:(UIImage *)image toRadius:(float)radius { 

    // create image sized context 
    UIGraphicsBeginImageContext(image.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // add rounded rect clipping path to context using radius 
    CGRect imageBounds = CGRectMake(0, 0, image.size.width, image.size.height); 
    CGPathRef clippingPath = [UIBezierPath bezierPathWithRoundedRect:imageBounds cornerRadius:radius].CGPath; 
    CGContextAddPath(context, clippingPath); 
    CGContextClip(context); 

    // draw the image 
    [image drawInRect:imageBounds]; 

    // get the image 
    UIImage *outImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return outImage; 
}