Added UIImage Category #2

Closed
wants to merge 4 commits into
from
@@ -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;
};
@@ -0,0 +1,16 @@
+//
+// UIColor+KGNoise.h
+// KGNoiseExample
+//
+// Created by Cory Imdieke on 9/14/12.
+// Copyright (c) 2012 BitSuites, LLC. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@interface UIColor (KGNoise)
+
+- (UIColor *)colorWithNoiseOpacity:(CGFloat)opacity;
+- (UIColor *)colorWithNoiseOpacity:(CGFloat)opacity andBlendMode:(CGBlendMode)blendMode;
+
+@end
@@ -0,0 +1,56 @@
+//
+// UIColor+KGNoise.m
+// KGNoiseExample
+//
+// Created by Cory Imdieke on 9/14/12.
+// Copyright (c) 2012 BitSuites, LLC. All rights reserved.
+//
+
+#import "UIColor+KGNoise.h"
+#import "KGNoise.h"
+
+@implementation UIColor (KGNoise)
+
+- (UIColor *)colorWithNoiseOpacity:(CGFloat)opacity{
+ return [self colorWithNoiseOpacity:opacity andBlendMode:kCGBlendModeScreen];
+}
+
+- (UIColor *)colorWithNoiseOpacity:(CGFloat)opacity andBlendMode:(CGBlendMode)blendMode{
+ // Figure out our screen scale, if it's a retina display we'll make the noise at twice the resolution
+ CGFloat screenScale = [[UIScreen mainScreen] scale];
+
+ // Create a context to draw in
+ CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
+ CGContextRef context = CGBitmapContextCreate(NULL,
+ 256.0 * screenScale,
+ 256.0 * screenScale,
+ 8, /* bits per channel */
+ (256.0 * screenScale * 4), /* 4 channels per pixel * numPixels/row */
+ colorSpace,
+ kCGImageAlphaPremultipliedLast);
+ CGColorSpaceRelease(colorSpace);
+
+ UIGraphicsPushContext(context);
+
+ // Fill with the color
+ CGContextSetFillColorWithColor(context, [self CGColor]);
+ CGContextFillRect(context, CGRectMake(0.0, 0.0, 256.0 * screenScale, 256.0 * screenScale));
+
+ // 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);
+
+ UIColor *patternColor = [UIColor colorWithPatternImage:finishedImage];
+
+ return patternColor;
+}
+
+@end
@@ -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
@@ -0,0 +1,60 @@
+//
+// 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.scale,
+ self.size.height * self.scale,
+ 8, /* bits per channel */
+ (self.size.width * self.scale * 4), /* 4 channels per pixel * numPixels/row */
+ colorSpace,
+ kCGImageAlphaPremultipliedLast);
+ CGColorSpaceRelease(colorSpace);
+
+ CGContextSaveGState(context);
+
+ // Flip context
+ CGContextTranslateCTM(context, 0, self.size.height * self.scale);
+ CGContextScaleCTM(context, self.scale, -self.scale);
+
+ UIGraphicsPushContext(context);
+
+ // Draw the image
+ [self drawAtPoint:CGPointMake(0.0, 0.0)];
+
+ CGContextRestoreGState(context);
+
+ CGContextClipToMask(context, CGRectMake(0.0, 0.0, self.size.width * self.scale, self.size.height * self.scale), [self CGImage]);
+
+ // 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 scale:self.scale orientation:self.imageOrientation];
+ CGImageRelease(rawImage);
+
+ return finishedImage;
+}
+
+@end