Created
August 6, 2012 21:55
-
-
Save travisjeffery/3278809 to your computer and use it in GitHub Desktop.
Mask and Shadow an Image in iOS (Masked into Circle With Shadow)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TJViewController.m | |
// MaskAndShadow | |
// | |
// Created by Travis Jeffery on 2012-08-06. | |
// Copyright (c) 2012 Travis Jeffery. All rights reserved. | |
// | |
#import "TJViewController.h" | |
#import <QuartzCore/QuartzCore.h> | |
@interface TJViewController () | |
@end | |
@implementation TJViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// white bg to see the shadow easier | |
self.view.backgroundColor = [UIColor whiteColor]; | |
// the image we're going to mask and shadow | |
UIImageView* image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sj.jpeg"]]; | |
image.center = self.view.center; | |
// make new layer to contain shadow and masked image | |
CALayer* containerLayer = [CALayer layer]; | |
containerLayer.shadowColor = [UIColor blackColor].CGColor; | |
containerLayer.shadowRadius = 10.f; | |
containerLayer.shadowOffset = CGSizeMake(0.f, 5.f); | |
containerLayer.shadowOpacity = 1.f; | |
// use the image's layer to mask the image into a circle | |
image.layer.cornerRadius = roundf(image.frame.size.width/2.0); | |
image.layer.masksToBounds = YES; | |
// add masked image layer into container layer so that it's shadowed | |
[containerLayer addSublayer:image.layer]; | |
// add container including masked image and shadow into view | |
[self.view.layer addSublayer:containerLayer]; | |
} | |
- (void)viewDidUnload | |
{ | |
[super viewDidUnload]; | |
// Release any retained subviews of the main view. | |
} | |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation | |
{ | |
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { | |
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); | |
} else { | |
return YES; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment