2009-07-01 78 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 
相關問題