The "Photos" button, as in contacts and the Facebook application

How can I create a "person photo" button, for example, in contact information or in the Facebook application? (gray and rounded border with a small radius and the image that is cropped inside it)

Edit:

Obviously, it works for all photos, not just what I got in Photoshop.

I think I could do it manually using masks, etc., but the Facebook application does it exactly the same as the contacts application, so I suspect there is some way to do it in the SDK.

+3
source share
2 answers

Here you (I: P) go:

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

.

, 2 ( 1px ) - . ~ 0,5 , , 1, . , :/

+2

(, "person.png" ), :

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

Source: https://habr.com/ru/post/1711702/


All Articles