forked from kgn/KGNoise
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added UIImage category to apply noise to any image
- Loading branch information
Cory Imdieke
committed
Sep 13, 2012
1 parent
0a3622d
commit 3d3cb5c
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// UIImage+KGNoise.h | ||
// KGNoiseExample | ||
// | ||
// Created by Cory Imdieke on 9/13/12. | ||
// Copyright (c) 2012 BitSuites, LLC. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
@interface UIImage (KGNoise) | ||
|
||
- (UIImage *)imageWithNoiseOpacity:(CGFloat)opacity; | ||
- (UIImage *)imageWithNoiseOpacity:(CGFloat)opacity andBlendMode:(CGBlendMode)blendMode; | ||
|
||
@end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// UIImage+KGNoise.m | ||
// KGNoiseExample | ||
// | ||
// Created by Cory Imdieke on 9/13/12. | ||
// Copyright (c) 2012 BitSuites, LLC. All rights reserved. | ||
// | ||
|
||
#import "UIImage+KGNoise.h" | ||
#import "KGNoise.h" | ||
|
||
@implementation UIImage (KGNoise) | ||
|
||
- (UIImage *)imageWithNoiseOpacity:(CGFloat)opacity{ | ||
return [self imageWithNoiseOpacity:opacity andBlendMode:kCGBlendModeScreen]; | ||
} | ||
|
||
- (UIImage *)imageWithNoiseOpacity:(CGFloat)opacity andBlendMode:(CGBlendMode)blendMode{ | ||
// Create a context to draw in | ||
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | ||
CGContextRef context = CGBitmapContextCreate(NULL, | ||
self.size.width, | ||
self.size.height, | ||
8, /* bits per channel */ | ||
(self.size.width * 4), /* 4 channels per pixel * numPixels/row */ | ||
colorSpace, | ||
kCGImageAlphaPremultipliedLast); | ||
CGColorSpaceRelease(colorSpace); | ||
|
||
// Flip context | ||
CGContextTranslateCTM(context, 0, self.size.height); | ||
CGContextScaleCTM(context, 1.0, -1.0); | ||
|
||
UIGraphicsPushContext(context); | ||
|
||
// Draw the image | ||
[self drawAtPoint:CGPointMake(0.0, 0.0)]; | ||
|
||
// Noise on top | ||
[KGNoise drawNoiseWithOpacity:opacity andBlendMode:blendMode]; | ||
|
||
// Create a CGImage from the context | ||
CGImageRef rawImage = CGBitmapContextCreateImage(context); | ||
UIGraphicsPopContext(); | ||
CGContextRelease(context); | ||
|
||
// Create a UIImage from the CGImage | ||
UIImage *finishedImage = [UIImage imageWithCGImage:rawImage]; | ||
CGImageRelease(rawImage); | ||
|
||
return finishedImage; | ||
} | ||
|
||
@end |