【问题标题】:Photo button like in contacts and Facebook app联系人和 Facebook 应用中的照片按钮
【发布时间】:2010-11-07 11:18:16
【问题描述】:

如何在联系信息或 Facebook 应用中创建“人物照片”按钮? (灰色圆角边框,小半径和在其中裁剪的图像)

编辑:

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

我想我可以使用掩码等手动完成,但 Facebook 应用完全像联系人应用一样,所以我怀疑 SDK 中有某种方法可以做到这一点。

【问题讨论】:

    标签: iphone objective-c cocoa-touch


    【解决方案1】:

    给你(我: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 落在剪辑区域之外)。理想情况下,边框的 alpha 约为 0.5,但由于抗锯齿为 2 个像素中的每一个像素提供了一些 alpha,因此我将其设置为 1,结果几乎正确。如果我禁用抗锯齿,那么拐角不会完全一样:/

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-05
        • 2013-06-29
        • 1970-01-01
        • 2014-01-14
        相关资源
        最近更新 更多