2009-07-01 84 views
3

如何创建联系人信息或Facebook应用程序中的“人照片”按钮? (半径较小,并且是里面冒出的图像灰度和圆角的边框)像联系人和Facebook应用程序中的照片按钮

编辑:

必须显然对所有照片的工作,不只是一个,我在Photoshop预渲染。

我想我可以手动使用面具等,但Facebook应用程序正好像联系人应用程序,所以我怀疑有一些方法可以在SDK中执行它。

回答

2

在这里,你(I:P)去:

+ (UIImage *)imageWithBorderForImage:(UIImage *)image 
{ 
    CGFloat radius = 5; 
    CGSize size = image.size; 

    radius = MIN(radius, .5 * MIN(size.width, size.height)); 
    CGRect interiorRect = CGRectInset(CGRectMake(0, 0, size.width, size.height), radius, radius); 

    UIGraphicsBeginImageContext(size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSaveGState(context); 

    CGMutablePathRef borderPath = CGPathCreateMutable(); 
    CGPathAddArc(borderPath, NULL, CGRectGetMinX(interiorRect), CGRectGetMinY(interiorRect), radius, 1.0*M_PI, 1.5*M_PI, NO); 
    CGPathAddArc(borderPath, NULL, CGRectGetMaxX(interiorRect), CGRectGetMinY(interiorRect), radius, 1.5*M_PI, 0.0*M_PI, NO); 
    CGPathAddArc(borderPath, NULL, CGRectGetMaxX(interiorRect), CGRectGetMaxY(interiorRect), radius, 0.0*M_PI, 0.5*M_PI, NO); 
    CGPathAddArc(borderPath, NULL, CGRectGetMinX(interiorRect), CGRectGetMaxY(interiorRect), radius, 0.5*M_PI, 1.0*M_PI, NO); 

    CGContextBeginPath(context); 
    CGContextAddPath(context, borderPath); 
    CGContextClosePath(context); 
    CGContextClip(context); 

    [image drawAtPoint:CGPointMake(0,0)]; 

    CGContextBeginPath(context); 
    CGContextAddPath(context, borderPath); 
    CGContextClosePath(context); 

    CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 1.0); 
    CGContextSetLineWidth(context, 1.0); 
    CGContextStrokePath(context); 

    CGPathRelease(borderPath); 

    UIImage *borderedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    CGContextRestoreGState(context); 
    UIGraphicsEndImageContext(); 

    return borderedImage; 
} 

主要依据在this question

一个问题是,由于抗锯齿,边框实际上是2px宽(尽管1px落在剪辑区域之外)。理想情况下,边界将有〜0.5 alpha,但由于抗锯齿给出了2个像素中的每一个alpha,所以我将它设置为1,并且它恰好出现在右边。如果我禁用了抗锯齿功能,那么拐角不会完全相同:/

1

创建为图像(如 “person.png”),然后加载它是这样的:

UIImage *personImage = [UIImage imageNamed:@"person.png"]; 
[[myButton imageView] setImage:personImage]; 
//where myButton is a UIButton of type UIButtonTypeCustom 
相关问题