objective C实现iPhone图标的水晶立体效果
- (void)viewDidLoad { [super viewDidLoad]; UIGraphicsBeginImageContext(icon.bounds.size); CGContextRef ctx = UIGraphicsGetCurrentContext(); const CGFloat components[4] = {0.0,0.4,0.0,1.0}; CGContextSetFillColor(ctx, components); CGContextFillRect(ctx, CGRectMake(0, 0, icon.bounds.size.width, icon.bounds.size.height)); UIImage *background = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImage *image = [UIImage imageNamed:@"icon.png"]; UIImage *mask = [UIImage imageNamed:@"IconBase.png"]; UIImage *roundCorner = [UIImage imageNamed:@"round-corner.png"]; icon.image = image; CALayer* subLayer = [[CALayer layer] retain]; subLayer.frame = icon.bounds; subLayer.contents = (id)[background CGImage]; CALayer* maskLayer = [[CALayer layer] retain]; maskLayer.frame = icon.bounds; maskLayer.contents = (id)[mask CGImage]; [subLayer setMask:maskLayer]; [[icon layer] addSublayer:subLayer]; CALayer* roundCornerLayer = [[CALayer layer] retain]; roundCornerLayer.frame = icon.bounds; roundCornerLayer.contents = (id)[roundCorner CGImage]; [[icon layer] setMask:roundCornerLayer]; [maskLayer release]; [subLayer release]; [roundCornerLayer release]; }