Permalink
Browse files

Added UIImage category to apply noise to any image

  • Loading branch information...
1 parent 0a3622d commit 3d3cb5c87d78eb7fae15aa1883c07442a19af1b9 Cory Imdieke committed Sep 13, 2012
Showing with 76 additions and 0 deletions.
  1. +6 −0 Example/KGNoiseExample.xcodeproj/project.pbxproj
  2. +16 −0 Example/UIImage+KGNoise.h
  3. +54 −0 Example/UIImage+KGNoise.m
@@ -18,6 +18,7 @@
73CF1C81160069A1006D9180 /* iOSMain.m in Sources */ = {isa = PBXBuildFile; fileRef = 73CF1C7C160069A1006D9180 /* iOSMain.m */; };
73CF1C82160069A1006D9180 /* KGIOSAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 73CF1C7E160069A1006D9180 /* KGIOSAppDelegate.m */; };
73CF1C8416006B6B006D9180 /* KGNoise.m in Sources */ = {isa = PBXBuildFile; fileRef = 73CF1C46160061B7006D9180 /* KGNoise.m */; };
+ 98FA88611602A22A0095CAB5 /* UIImage+KGNoise.m in Sources */ = {isa = PBXBuildFile; fileRef = 98FA88601602A22A0095CAB5 /* UIImage+KGNoise.m */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@@ -38,6 +39,8 @@
73CF1C7C160069A1006D9180 /* iOSMain.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = iOSMain.m; sourceTree = "<group>"; };
73CF1C7D160069A1006D9180 /* KGIOSAppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KGIOSAppDelegate.h; sourceTree = "<group>"; };
73CF1C7E160069A1006D9180 /* KGIOSAppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KGIOSAppDelegate.m; sourceTree = "<group>"; };
+ 98FA885F1602A22A0095CAB5 /* UIImage+KGNoise.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+KGNoise.h"; sourceTree = "<group>"; };
+ 98FA88601602A22A0095CAB5 /* UIImage+KGNoise.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+KGNoise.m"; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -67,6 +70,8 @@
children = (
73CF1C45160061B7006D9180 /* KGNoise.h */,
73CF1C46160061B7006D9180 /* KGNoise.m */,
+ 98FA885F1602A22A0095CAB5 /* UIImage+KGNoise.h */,
+ 98FA88601602A22A0095CAB5 /* UIImage+KGNoise.m */,
73CF1C3C160060C5006D9180 /* Mac Example */,
73CF1C7B16006936006D9180 /* iOS Example */,
73CF1C171600604E006D9180 /* Products */,
@@ -226,6 +231,7 @@
73CF1C81160069A1006D9180 /* iOSMain.m in Sources */,
73CF1C82160069A1006D9180 /* KGIOSAppDelegate.m in Sources */,
73CF1C8416006B6B006D9180 /* KGNoise.m in Sources */,
+ 98FA88611602A22A0095CAB5 /* UIImage+KGNoise.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
@@ -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
View
@@ -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

0 comments on commit 3d3cb5c

Please sign in to comment.