Iphone 联系人和Facebook应用程序中的照片按钮

Iphone 联系人和Facebook应用程序中的照片按钮,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,如何创建“个人照片”按钮,如联系人信息或Facebook应用程序中的按钮?(带有小半径的灰色圆形边框和内部裁剪的图像) 编辑: 显然,它必须适用于所有照片,而不仅仅是我在Photoshop中预渲染的照片 我想我可以使用掩码等手动操作,但Facebook应用程序的操作与contacts应用程序完全相同,因此我怀疑SDK中有某种方法可以做到这一点。将其创建为图像(如“person.png”),然后按如下方式加载: UIImage *personImage = [UIImage imageNamed:

如何创建“个人照片”按钮,如联系人信息或Facebook应用程序中的按钮?(带有小半径的灰色圆形边框和内部裁剪的图像)

编辑:

显然,它必须适用于所有照片,而不仅仅是我在Photoshop中预渲染的照片

我想我可以使用掩码等手动操作,但Facebook应用程序的操作与contacts应用程序完全相同,因此我怀疑SDK中有某种方法可以做到这一点。

将其创建为图像(如“person.png”),然后按如下方式加载:

UIImage *personImage = [UIImage imageNamed:@"person.png"];
[[myButton imageView] setImage:personImage];
//where myButton is a UIButton of type UIButtonTypeCustom
给你(I:p)去:

主要基于

一个问题是,由于抗锯齿,边界实际上是2px宽(尽管1px位于剪辑区域之外)。理想情况下,边框的alpha值为0.5左右,但由于抗锯齿使2个像素中的每个像素都有一些alpha值,因此我将其设置为1,结果几乎正确。如果禁用抗锯齿,则角不会完全圆角:/

+ (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;
}