Last active
August 29, 2015 14:14
-
-
Save manmal/2c3573a6341d4436ba85 to your computer and use it in GitHub Desktop.
iOS Gradient with noise overlaid (like https://www.dropbox.com/s/6a6lsqasee0zp1q/bgview.png?dl=0)
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
// Just a quick hack to achieve something like this: https://www.dropbox.com/s/6a6lsqasee0zp1q/bgview.png?dl=0 | |
@interface MMGradientBackgroundView() | |
@property (strong, nonatomic) UIImage *noiseImage; | |
@end | |
@implementation MMGradientBackgroundView | |
- (instancetype)initWithFrame:(CGRect)frame { | |
if (self = [super initWithFrame:frame]) { | |
[self commonInit]; | |
} | |
return self; | |
} | |
- (instancetype)init { | |
if (self = [super init]) { | |
[self commonInit]; | |
} | |
return self; | |
} | |
- (void)commonInit { | |
self.noiseImage = [UIImage imageNamed:@"[email protected]"]; // https://www.dropbox.com/s/t9ky8226eybqfia/noisemap%402x.png?dl=0 | |
} | |
- (void)drawRect:(CGRect)rect { | |
[super drawRect:rect]; | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGColorRef startColor = [UIColor colorWithWhite:.46f alpha:1.0f].CGColor; | |
CGColorRef endColor = [UIColor colorWithWhite:0.f alpha:0.f].CGColor; | |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | |
CGFloat locations[] = { 0.0f, 1.0f }; | |
NSArray *colors = @[(__bridge id)startColor, (__bridge id)endColor]; | |
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, locations); | |
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect)); | |
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect)); | |
CGContextSaveGState(context); | |
CGContextAddRect(context, rect); | |
CGContextClip(context); | |
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0); | |
CGContextRestoreGState(context); | |
CGGradientRelease(gradient); | |
CGColorSpaceRelease(colorSpace); | |
CGContextDrawTiledImage(context, CGRectMake(0, 0, self.noiseImage.size.width, self.noiseImage.size.height), self.noiseImage.CGImage); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment