Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 126 lines (109 sloc) 3.549 kb
d8d6de50 »
2012-09-11 initial commit of working mac example
1 //
2 // KGNoise.m
c76dd506 »
2012-09-12 adding example iOS app
3 // KGNoise
d8d6de50 »
2012-09-11 initial commit of working mac example
4 //
5 // Created by David Keegan on 9/11/12.
6 // Copyright (c) 2012 David Keegan. All rights reserved.
7 //
8
9 #import "KGNoise.h"
10
11 @implementation KGNoise
12
13 + (void)drawNoiseWithOpacity:(CGFloat)opacity{
14 [self drawNoiseWithOpacity:opacity andBlendMode:kCGBlendModeScreen];
15 }
16
17 + (void)drawNoiseWithOpacity:(CGFloat)opacity andBlendMode:(CGBlendMode)blendMode{
18 static CGImageRef noiseImageRef = nil;
19 static dispatch_once_t oncePredicate;
20 dispatch_once(&oncePredicate, ^{
ac1c7b93 »
2012-09-12 setting the image size to 128, as it should have been
21 NSUInteger width = 128, height = width;
d8d6de50 »
2012-09-11 initial commit of working mac example
22 NSUInteger size = width*height;
23 char *rgba = (char *)malloc(size); srand(115);
24 for(NSUInteger i=0; i < size; ++i){rgba[i] = rand()%256;}
25 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
26 CGContextRef bitmapContext =
27 CGBitmapContextCreate(rgba, width, height, 8, width, colorSpace, kCGImageAlphaNone);
28 CFRelease(colorSpace);
29 noiseImageRef = CGBitmapContextCreateImage(bitmapContext);
30 CFRelease(bitmapContext);
31 free(rgba);
32 });
33
34 #if TARGET_OS_IPHONE
35 CGContextRef context = UIGraphicsGetCurrentContext();
36 #else
37 CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
38 #endif
39
40 CGContextSaveGState(context);
41 CGContextSetAlpha(context, opacity);
42 CGContextSetBlendMode(context, blendMode);
43
44 #if TARGET_OS_IPHONE
45 if([[UIScreen mainScreen] respondsToSelector:@selector(scale)]){
46 CGFloat scaleFactor = [[UIScreen mainScreen] scale];
47 CGContextScaleCTM(context, 1/scaleFactor, 1/scaleFactor);
48 }
49 #else
50 if([[NSScreen mainScreen] respondsToSelector:@selector(backingScaleFactor)]){
51 CGFloat scaleFactor = [[NSScreen mainScreen] backingScaleFactor];
52 CGContextScaleCTM(context, 1/scaleFactor, 1/scaleFactor);
53 }
54 #endif
55
56 CGRect imageRect = (CGRect){CGPointZero, CGImageGetWidth(noiseImageRef), CGImageGetHeight(noiseImageRef)};
57 CGContextDrawTiledImage(context, imageRect, noiseImageRef);
58 CGContextRestoreGState(context);
59 }
60
61 @end
62
63 @implementation KGNoiseView
64
65 #if TARGET_OS_IPHONE
66 - (id)initWithFrame:(CGRect)frameRect{
67 #else
68 - (id)initWithFrame:(NSRect)frameRect{
69 #endif
70 if((self = [super initWithFrame:frameRect])){
71 [self setup];
72 }
73 return self;
74 }
75
76 - (id)initWithCoder:(NSCoder *)aDecoder{
77 if((self = [super initWithCoder:aDecoder])){
78 [self setup];
79 }
80 return self;
81 }
82
83 - (void)setup{
84 #if TARGET_OS_IPHONE
c76dd506 »
2012-09-12 adding example iOS app
85 self.backgroundColor = [UIColor grayColor];
d8d6de50 »
2012-09-11 initial commit of working mac example
86 #else
c76dd506 »
2012-09-12 adding example iOS app
87 self.backgroundColor = [NSColor grayColor];
d8d6de50 »
2012-09-11 initial commit of working mac example
88 #endif
89 self.noiseOpacity = 0.1;
90 self.noiseBlendMode = kCGBlendModeScreen;
91 }
92
93 #if TARGET_OS_IPHONE
94 #else
95 - (void)setBackgroundColor:(NSColor *)backgroundColor{
96 if(_backgroundColor != backgroundColor){
97 _backgroundColor = backgroundColor;
98 [self setNeedsDisplay:YES];
99 }
100 }
101 #endif
102
103 - (void)setNoiseOpacity:(CGFloat)noiseOpacity{
104 if(_noiseOpacity != noiseOpacity){
105 _noiseOpacity = noiseOpacity;
106 #if TARGET_OS_IPHONE
107 [self setNeedsDisplay];
108 #else
109 [self setNeedsDisplay:YES];
110 #endif
111 }
112 }
113
114 #if TARGET_OS_IPHONE
115 - (void)drawRect:(CGRect)dirtyRect{
116 CGContextRef context = UIGraphicsGetCurrentContext();
117 #else
118 - (void)drawRect:(NSRect)dirtyRect{
119 CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
120 #endif
121 [self.backgroundColor setFill];
122 CGContextFillRect(context, self.bounds);
123 [KGNoise drawNoiseWithOpacity:self.noiseOpacity andBlendMode:self.noiseBlendMode];
124 }
125
126 @end
Something went wrong with that request. Please try again.