10 |
10 |
11 |
11 |
12 @implementation UIImage (extra) |
12 @implementation UIImage (extra) |
13 |
13 |
14 -(UIImage *)scaleToSize:(CGSize) size { |
14 -(UIImage *)scaleToSize:(CGSize) size { |
15 // Create a bitmap graphics context |
15 DLog(@"warning - this is a very expensive operation, you should avoid using it"); |
16 // This will also set it as the current context |
16 |
17 UIGraphicsBeginImageContext(size); |
17 // Create a bitmap graphics context; this will also set it as the current context |
18 |
18 UIGraphicsBeginImageContext(size); |
19 // Draw the scaled image in the current context |
19 |
20 [self drawInRect:CGRectMake(0, 0, size.width, size.height)]; |
20 // Draw the scaled image in the current context |
21 |
21 [self drawInRect:CGRectMake(0, 0, size.width, size.height)]; |
22 // Create a new image from current context |
22 |
23 UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); |
23 // Create a new image from current context |
24 |
24 UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); |
25 // Pop the current context from the stack |
25 |
26 UIGraphicsEndImageContext(); |
26 // Pop the current context from the stack |
27 |
27 UIGraphicsEndImageContext(); |
28 // Return our new scaled image (autoreleased) |
28 |
29 return scaledImage; |
29 // Return our new scaled image (autoreleased) |
|
30 return scaledImage; |
30 } |
31 } |
31 |
32 |
32 -(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint { |
33 -(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint { |
33 // create a contex of size of the background image |
34 // create a contex of size of the background image |
34 return [self mergeWith:secondImage atPoint:secondImagePoint atSize:self.size]; |
35 return [self mergeWith:secondImage atPoint:secondImagePoint atSize:self.size]; |
35 } |
36 } |
36 |
37 |
37 -(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint atSize:(CGSize) resultingSize { |
38 -(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint atSize:(CGSize) resultingSize { |
|
39 // Create a bitmap graphics context; this will also set it as the current context |
38 UIGraphicsBeginImageContext(resultingSize); |
40 UIGraphicsBeginImageContext(resultingSize); |
39 |
41 |
40 // drav the background image |
42 // draw the background image in the current context |
41 [self drawAtPoint:CGPointMake(0,0)]; |
43 [self drawAtPoint:CGPointMake(0,0)]; |
42 |
44 |
43 // draw the image on top of the first image |
45 // draw the image on top of the first image (because the context is the same) |
44 [secondImage drawAtPoint:secondImagePoint]; |
46 [secondImage drawAtPoint:secondImagePoint]; |
45 |
47 |
46 // create an image from the current contex (not thread safe) |
48 // create an image from the current contex (not thread safe) |
47 UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); |
49 UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); |
48 |
50 |
77 return nil; |
79 return nil; |
78 } |
80 } |
79 } |
81 } |
80 |
82 |
81 -(UIImage *)convertToGrayScale { |
83 -(UIImage *)convertToGrayScale { |
82 // Create image rectangle with current image width/height |
84 // Create image rectangle with current image width/height |
83 CGRect imageRect = CGRectMake(0, 0, self.size.width, self.size.height); |
85 CGRect imageRect = CGRectMake(0, 0, self.size.width, self.size.height); |
84 |
86 |
85 // Grayscale color space |
87 // Grayscale color space |
86 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); |
88 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); |
87 |
89 |
88 // Create bitmap content with current image size and grayscale colorspace |
90 // Create bitmap content with current image size and grayscale colorspace |
89 CGContextRef context = CGBitmapContextCreate(nil, self.size.width, self.size.height, 8, 0, colorSpace, kCGImageAlphaNone); |
91 CGContextRef context = CGBitmapContextCreate(nil, self.size.width, self.size.height, 8, 0, colorSpace, kCGImageAlphaNone); |
90 |
92 |
91 // Draw image into current context, with specified rectangle |
93 // Draw image into current context, with specified rectangle |
92 // using previously defined context (with grayscale colorspace) |
94 // using previously defined context (with grayscale colorspace) |
93 CGContextDrawImage(context, imageRect, [self CGImage]); |
95 CGContextDrawImage(context, imageRect, [self CGImage]); |
94 |
96 |
95 // Create bitmap image info from pixel data in current context |
97 // Create bitmap image info from pixel data in current context |
96 CGImageRef imageRef = CGBitmapContextCreateImage(context); |
98 CGImageRef imageRef = CGBitmapContextCreateImage(context); |
97 |
99 |
98 // Create a new UIImage object |
100 // Create a new UIImage object |
99 UIImage *newImage = [UIImage imageWithCGImage:imageRef]; |
101 UIImage *newImage = [UIImage imageWithCGImage:imageRef]; |
100 |
102 |
101 // Release colorspace, context and bitmap information |
103 // Release colorspace, context and bitmap information |
102 CGColorSpaceRelease(colorSpace); |
104 CGColorSpaceRelease(colorSpace); |
103 CGContextRelease(context); |
105 CGContextRelease(context); |
104 CFRelease(imageRef); |
106 CFRelease(imageRef); |
105 |
107 |
106 // Return the new grayscale image |
108 // Return the new grayscale image |
107 return newImage; |
109 return newImage; |
108 } |
110 } |
109 |
111 |
110 // by http://iphonedevelopertips.com/cocoa/how-to-mask-an-image.html turned into a category by koda |
112 // by http://iphonedevelopertips.com/cocoa/how-to-mask-an-image.html turned into a category by koda |
111 -(UIImage*) maskImageWith:(UIImage *)maskImage { |
113 -(UIImage*) maskImageWith:(UIImage *)maskImage { |
112 CGImageRef maskRef = maskImage.CGImage; |
114 // prepare the reference image |
|
115 CGImageRef maskRef = [maskImage CGImage]; |
113 |
116 |
|
117 // create the mask using parameters of the mask reference |
114 CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), |
118 CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), |
115 CGImageGetHeight(maskRef), |
119 CGImageGetHeight(maskRef), |
116 CGImageGetBitsPerComponent(maskRef), |
120 CGImageGetBitsPerComponent(maskRef), |
117 CGImageGetBitsPerPixel(maskRef), |
121 CGImageGetBitsPerPixel(maskRef), |
118 CGImageGetBytesPerRow(maskRef), |
122 CGImageGetBytesPerRow(maskRef), |
119 CGImageGetDataProvider(maskRef), NULL, false); |
123 CGImageGetDataProvider(maskRef), NULL, false); |
120 |
124 |
|
125 // create an image in the current context |
121 CGImageRef masked = CGImageCreateWithMask([self CGImage], mask); |
126 CGImageRef masked = CGImageCreateWithMask([self CGImage], mask); |
122 |
|
123 CGImageRelease(mask); |
127 CGImageRelease(mask); |
124 |
128 |
125 UIImage* retImage = [UIImage imageWithCGImage:masked]; |
129 UIImage* retImage = [UIImage imageWithCGImage:masked]; |
126 |
|
127 CGImageRelease(masked); |
130 CGImageRelease(masked); |
128 |
131 |
129 return retImage; |
132 return retImage; |
130 } |
133 } |
131 |
134 |
132 // by http://blog.sallarp.com/iphone-uiimage-round-corners/ turned into a category by koda |
135 // 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) |
136 void addRoundedRectToPath(CGContextRef context, CGRect rect, CGFloat ovalWidth, CGFloat ovalHeight) { |
134 { |
137 CGFloat fw, fh; |
135 float fw, fh; |
|
136 if (ovalWidth == 0 || ovalHeight == 0) { |
138 if (ovalWidth == 0 || ovalHeight == 0) { |
137 CGContextAddRect(context, rect); |
139 CGContextAddRect(context, rect); |
138 return; |
140 return; |
139 } |
141 } |
140 CGContextSaveGState(context); |
142 CGContextSaveGState(context); |
149 CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); |
151 CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); |
150 CGContextClosePath(context); |
152 CGContextClosePath(context); |
151 CGContextRestoreGState(context); |
153 CGContextRestoreGState(context); |
152 } |
154 } |
153 |
155 |
154 -(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh { |
156 -(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh { |
155 UIImage * newImage = nil; |
157 CGFloat cornerWidth = sizewh.width; |
156 |
158 CGFloat cornerHeight = sizewh.height; |
157 NSInteger cornerWidth = sizewh.width; |
159 CGFloat w = self.size.width; |
158 NSInteger cornerHeight = sizewh.height; |
160 CGFloat h = self.size.height; |
159 int w = self.size.width; |
|
160 int h = self.size.height; |
|
161 |
161 |
162 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
162 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
163 CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); |
163 CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); |
164 |
164 |
165 CGContextBeginPath(context); |
165 CGContextBeginPath(context); |
166 CGRect rect = CGRectMake(0, 0, w, h); |
166 CGRect rect = CGRectMake(0, 0, w, h); |
167 addRoundedRectToPath(context, rect, cornerWidth, cornerHeight); |
167 addRoundedRectToPath(context, rect, cornerWidth, cornerHeight); |
168 CGContextClosePath(context); |
168 CGContextClosePath(context); |
169 CGContextClip(context); |
169 CGContextClip(context); |
170 |
170 |
171 CGContextDrawImage(context, CGRectMake(0, 0, w, h), self.CGImage); |
171 CGContextDrawImage(context, CGRectMake(0, 0, w, h), [self CGImage]); |
172 |
172 |
173 CGImageRef imageMasked = CGBitmapContextCreateImage(context); |
173 CGImageRef imageMasked = CGBitmapContextCreateImage(context); |
174 CGContextRelease(context); |
174 CGContextRelease(context); |
175 CGColorSpaceRelease(colorSpace); |
175 CGColorSpaceRelease(colorSpace); |
176 |
176 |
177 newImage = [UIImage imageWithCGImage:imageMasked]; |
177 UIImage *newImage = [UIImage imageWithCGImage:imageMasked]; |
178 CGImageRelease(imageMasked); |
178 CGImageRelease(imageMasked); |
179 |
179 |
180 return newImage; |
180 return newImage; |
181 } |
181 } |
182 |
182 |
183 |
|
184 @end |
183 @end |