76 NSLog(@"initWithContentsOfFile: andCutAt: FAILED"); |
76 NSLog(@"initWithContentsOfFile: andCutAt: FAILED"); |
77 return nil; |
77 return nil; |
78 } |
78 } |
79 } |
79 } |
80 |
80 |
81 -(UIImage *)convertImageToGrayScale:(UIImage *)image { |
81 -(UIImage *)convertToGrayScale { |
82 // Create image rectangle with current image width/height |
82 // Create image rectangle with current image width/height |
83 CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height); |
83 CGRect imageRect = CGRectMake(0, 0, self.size.width, self.size.height); |
84 |
84 |
85 // Grayscale color space |
85 // Grayscale color space |
86 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); |
86 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); |
87 |
87 |
88 // Create bitmap content with current image size and grayscale colorspace |
88 // Create bitmap content with current image size and grayscale colorspace |
89 CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone); |
89 CGContextRef context = CGBitmapContextCreate(nil, self.size.width, self.size.height, 8, 0, colorSpace, kCGImageAlphaNone); |
90 |
90 |
91 // Draw image into current context, with specified rectangle |
91 // Draw image into current context, with specified rectangle |
92 // using previously defined context (with grayscale colorspace) |
92 // using previously defined context (with grayscale colorspace) |
93 CGContextDrawImage(context, imageRect, [image CGImage]); |
93 CGContextDrawImage(context, imageRect, [self CGImage]); |
94 |
94 |
95 // Create bitmap image info from pixel data in current context |
95 // Create bitmap image info from pixel data in current context |
96 CGImageRef imageRef = CGBitmapContextCreateImage(context); |
96 CGImageRef imageRef = CGBitmapContextCreateImage(context); |
97 |
97 |
98 // Create a new UIImage object |
98 // Create a new UIImage object |
104 CFRelease(imageRef); |
104 CFRelease(imageRef); |
105 |
105 |
106 // Return the new grayscale image |
106 // Return the new grayscale image |
107 return newImage; |
107 return newImage; |
108 } |
108 } |
109 |
109 |
|
110 // by http://iphonedevelopertips.com/cocoa/how-to-mask-an-image.html turned into a category by koda |
|
111 -(UIImage*) maskImageWith:(UIImage *)maskImage { |
|
112 CGImageRef maskRef = maskImage.CGImage; |
|
113 |
|
114 CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), |
|
115 CGImageGetHeight(maskRef), |
|
116 CGImageGetBitsPerComponent(maskRef), |
|
117 CGImageGetBitsPerPixel(maskRef), |
|
118 CGImageGetBytesPerRow(maskRef), |
|
119 CGImageGetDataProvider(maskRef), NULL, false); |
|
120 |
|
121 CGImageRef masked = CGImageCreateWithMask([self CGImage], mask); |
|
122 |
|
123 CGImageRelease(mask); |
|
124 |
|
125 UIImage* retImage = [UIImage imageWithCGImage:masked]; |
|
126 |
|
127 CGImageRelease(masked); |
|
128 |
|
129 return retImage; |
|
130 } |
|
131 |
|
132 // by http://blog.sallarp.com/iphone-uiimage-round-corners/ turned into a category by koda |
|
133 void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth, float ovalHeight) |
|
134 { |
|
135 float fw, fh; |
|
136 if (ovalWidth == 0 || ovalHeight == 0) { |
|
137 CGContextAddRect(context, rect); |
|
138 return; |
|
139 } |
|
140 CGContextSaveGState(context); |
|
141 CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect)); |
|
142 CGContextScaleCTM (context, ovalWidth, ovalHeight); |
|
143 fw = CGRectGetWidth (rect) / ovalWidth; |
|
144 fh = CGRectGetHeight (rect) / ovalHeight; |
|
145 CGContextMoveToPoint(context, fw, fh/2); |
|
146 CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); |
|
147 CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); |
|
148 CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); |
|
149 CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); |
|
150 CGContextClosePath(context); |
|
151 CGContextRestoreGState(context); |
|
152 } |
|
153 |
|
154 -(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh { |
|
155 UIImage * newImage = nil; |
|
156 |
|
157 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
|
158 |
|
159 NSInteger cornerWidth = sizewh.width; |
|
160 NSInteger cornerHeight = sizewh.height; |
|
161 int w = self.size.width; |
|
162 int h = self.size.height; |
|
163 |
|
164 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
|
165 CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); |
|
166 |
|
167 CGContextBeginPath(context); |
|
168 CGRect rect = CGRectMake(0, 0, w, h); |
|
169 addRoundedRectToPath(context, rect, cornerWidth, cornerHeight); |
|
170 CGContextClosePath(context); |
|
171 CGContextClip(context); |
|
172 |
|
173 CGContextDrawImage(context, CGRectMake(0, 0, w, h), self.CGImage); |
|
174 |
|
175 CGImageRef imageMasked = CGBitmapContextCreateImage(context); |
|
176 CGContextRelease(context); |
|
177 CGColorSpaceRelease(colorSpace); |
|
178 [self release]; |
|
179 |
|
180 newImage = [[UIImage imageWithCGImage:imageMasked] retain]; |
|
181 CGImageRelease(imageMasked); |
|
182 |
|
183 [pool release]; |
|
184 |
|
185 return newImage; |
|
186 } |
|
187 |
|
188 |
110 @end |
189 @end |