2013年1月15日星期二

[Tips] Show Circle in iOS app






first get baby icon,and add a circle crop,after that ,Superposition it with background image 
Iike this:


code 
double radians (double degrees) {return degrees * M_PI/180;}
-(UIImage*)createIconWithImage:(UIImage*)src
{
    UIImage *circle=[UIImage imageNamed:@"Face@2x.png"];
    CGFloat width=circle.size.width;
    CGFloat height=circle.size.height;
   
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedFirst);
    
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), circle.CGImage);
    
    if (src.imageOrientation == UIImageOrientationLeft) {
        CGContextRotateCTM (context, radians(90));
        CGContextTranslateCTM (context, 0, -height);
        
    } else if (src.imageOrientation == UIImageOrientationRight||src.imageOrientation == UIImageOrientationRightMirrored) {
        CGContextRotateCTM (context, radians(-90));
        CGContextTranslateCTM (context, -width, 0);
        
    } else if (src.imageOrientation == UIImageOrientationUp) {
        // NOTHING
    } else if (src.imageOrientation == UIImageOrientationDown) {
        CGContextTranslateCTM (context, width, height);
        CGContextRotateCTM (context, radians(-180.));
    }
    CGContextBeginPath(context);
    
    CGContextTranslateCTM (context, 4, 4);
    CGRect rect = CGRectMake(0, 0, width-12, height-12);
    CGContextAddEllipseInRect(context, rect);
    CGContextClosePath(context);
    CGContextClip(context);
    CGContextDrawImage(context, rect , src.CGImage);
    
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
UIImage *image = [UIImage imageWithCGImage:imageMasked];
CGImageRelease(imageMasked);
    return image;
}
I thank there has a good way do this work, I use code do this like first picture in this article.

sample code : 


#import <QuartzCore/QuartzCore.h>        
self.contentView.layer.cornerRadius = 35.0;
        self.contentView.layer.borderWidth = 1.0f;
        self.contentView.layer.borderColor = [UIColor blueColor].CGColor;
        self.contentView.layer.shadowColor = [UIColor blueColor].CGColor;
        self.contentView.layer.shadowOffset = CGSizeMake(2.0, 2.0);
        self.contentView.layer.shadowOpacity= 0.5;
        self.contentView.backgroundColor = [UIColor underPageBackgroundColor];


reference 
Apple CALayer Class Reference 

没有评论:

发表评论