3547
|
1 |
//
|
|
2 |
// UIImageExtra.m
|
|
3 |
// HedgewarsMobile
|
|
4 |
//
|
|
5 |
// Created by Vittorio on 08/04/10.
|
|
6 |
// Copyright 2010 __MyCompanyName__. All rights reserved.
|
|
7 |
//
|
|
8 |
|
|
9 |
#import "UIImageExtra.h"
|
|
10 |
|
|
11 |
|
|
12 |
@implementation UIImage (extra)
|
|
13 |
|
|
14 |
-(UIImage *)scaleToSize:(CGSize) size {
|
3573
|
15 |
DLog(@"warning - this is a very expensive operation, you should avoid using it");
|
|
16 |
|
|
17 |
// Create a bitmap graphics context; this will also set it as the current context
|
|
18 |
UIGraphicsBeginImageContext(size);
|
|
19 |
|
|
20 |
// Draw the scaled image in the current context
|
|
21 |
[self drawInRect:CGRectMake(0, 0, size.width, size.height)];
|
|
22 |
|
|
23 |
// Create a new image from current context
|
|
24 |
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
|
|
25 |
|
|
26 |
// Pop the current context from the stack
|
|
27 |
UIGraphicsEndImageContext();
|
|
28 |
|
|
29 |
// Return our new scaled image (autoreleased)
|
|
30 |
return scaledImage;
|
3547
|
31 |
}
|
|
32 |
|
|
33 |
-(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint {
|
|
34 |
// create a contex of size of the background image
|
|
35 |
return [self mergeWith:secondImage atPoint:secondImagePoint atSize:self.size];
|
|
36 |
}
|
|
37 |
|
|
38 |
-(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint atSize:(CGSize) resultingSize {
|
3573
|
39 |
// Create a bitmap graphics context; this will also set it as the current context
|
3547
|
40 |
UIGraphicsBeginImageContext(resultingSize);
|
|
41 |
|
3573
|
42 |
// draw the background image in the current context
|
3547
|
43 |
[self drawAtPoint:CGPointMake(0,0)];
|
|
44 |
|
3573
|
45 |
// draw the image on top of the first image (because the context is the same)
|
3547
|
46 |
[secondImage drawAtPoint:secondImagePoint];
|
|
47 |
|
|
48 |
// create an image from the current contex (not thread safe)
|
|
49 |
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
|
|
50 |
|
|
51 |
// free drawing contex
|
|
52 |
UIGraphicsEndImageContext();
|
|
53 |
|
|
54 |
// return the resulting autoreleased image
|
|
55 |
return resultImage;
|
|
56 |
}
|
|
57 |
|
|
58 |
-(id) initWithContentsOfFile:(NSString *)path andCutAt:(CGRect) rect {
|
|
59 |
// load image from path
|
|
60 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile: path];
|
|
61 |
|
|
62 |
if (nil != image) {
|
|
63 |
// get its CGImage representation with a give size
|
3621
|
64 |
CGImageRef cgImage = CGImageCreateWithImageInRect([image CGImage], rect);
|
3547
|
65 |
|
|
66 |
// clean memory
|
|
67 |
[image release];
|
|
68 |
|
|
69 |
// create a UIImage from the CGImage (memory must be allocated already)
|
3621
|
70 |
UIImage *sprite = [self initWithCGImage:cgImage];
|
3547
|
71 |
|
|
72 |
// clean memory
|
3621
|
73 |
CGImageRelease(cgImage);
|
3547
|
74 |
|
|
75 |
// return resulting image
|
|
76 |
return sprite;
|
|
77 |
} else {
|
|
78 |
DLog(@"error - image == nil");
|
|
79 |
return nil;
|
|
80 |
}
|
|
81 |
}
|
|
82 |
|
3621
|
83 |
-(UIImage *)cutAt:(CGRect) rect {
|
|
84 |
CGImageRef cgImage = CGImageCreateWithImageInRect([self CGImage], rect);
|
|
85 |
|
|
86 |
UIImage *res = [UIImage imageWithCGImage:cgImage];
|
|
87 |
CGImageRelease(cgImage);
|
|
88 |
|
|
89 |
return res;
|
|
90 |
}
|
|
91 |
|
3547
|
92 |
-(UIImage *)convertToGrayScale {
|
3573
|
93 |
// Create image rectangle with current image width/height
|
|
94 |
CGRect imageRect = CGRectMake(0, 0, self.size.width, self.size.height);
|
|
95 |
|
|
96 |
// Grayscale color space
|
|
97 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
|
|
98 |
|
|
99 |
// Create bitmap content with current image size and grayscale colorspace
|
|
100 |
CGContextRef context = CGBitmapContextCreate(nil, self.size.width, self.size.height, 8, 0, colorSpace, kCGImageAlphaNone);
|
|
101 |
|
|
102 |
// Draw image into current context, with specified rectangle
|
|
103 |
// using previously defined context (with grayscale colorspace)
|
|
104 |
CGContextDrawImage(context, imageRect, [self CGImage]);
|
|
105 |
|
|
106 |
// Create bitmap image info from pixel data in current context
|
|
107 |
CGImageRef imageRef = CGBitmapContextCreateImage(context);
|
|
108 |
|
|
109 |
// Create a new UIImage object
|
|
110 |
UIImage *newImage = [UIImage imageWithCGImage:imageRef];
|
|
111 |
|
|
112 |
// Release colorspace, context and bitmap information
|
|
113 |
CGColorSpaceRelease(colorSpace);
|
|
114 |
CGContextRelease(context);
|
|
115 |
CFRelease(imageRef);
|
|
116 |
|
|
117 |
// Return the new grayscale image
|
|
118 |
return newImage;
|
3547
|
119 |
}
|
|
120 |
|
|
121 |
// by http://iphonedevelopertips.com/cocoa/how-to-mask-an-image.html turned into a category by koda
|
|
122 |
-(UIImage*) maskImageWith:(UIImage *)maskImage {
|
3573
|
123 |
// prepare the reference image
|
|
124 |
CGImageRef maskRef = [maskImage CGImage];
|
3547
|
125 |
|
3573
|
126 |
// create the mask using parameters of the mask reference
|
3547
|
127 |
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
|
|
128 |
CGImageGetHeight(maskRef),
|
|
129 |
CGImageGetBitsPerComponent(maskRef),
|
|
130 |
CGImageGetBitsPerPixel(maskRef),
|
|
131 |
CGImageGetBytesPerRow(maskRef),
|
|
132 |
CGImageGetDataProvider(maskRef), NULL, false);
|
|
133 |
|
3573
|
134 |
// create an image in the current context
|
3547
|
135 |
CGImageRef masked = CGImageCreateWithMask([self CGImage], mask);
|
|
136 |
CGImageRelease(mask);
|
|
137 |
|
|
138 |
UIImage* retImage = [UIImage imageWithCGImage:masked];
|
|
139 |
CGImageRelease(masked);
|
|
140 |
|
|
141 |
return retImage;
|
|
142 |
}
|
|
143 |
|
|
144 |
// by http://blog.sallarp.com/iphone-uiimage-round-corners/ turned into a category by koda
|
3573
|
145 |
void addRoundedRectToPath(CGContextRef context, CGRect rect, CGFloat ovalWidth, CGFloat ovalHeight) {
|
|
146 |
CGFloat fw, fh;
|
3547
|
147 |
if (ovalWidth == 0 || ovalHeight == 0) {
|
|
148 |
CGContextAddRect(context, rect);
|
|
149 |
return;
|
|
150 |
}
|
|
151 |
CGContextSaveGState(context);
|
|
152 |
CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
|
|
153 |
CGContextScaleCTM (context, ovalWidth, ovalHeight);
|
|
154 |
fw = CGRectGetWidth (rect) / ovalWidth;
|
|
155 |
fh = CGRectGetHeight (rect) / ovalHeight;
|
|
156 |
CGContextMoveToPoint(context, fw, fh/2);
|
|
157 |
CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);
|
|
158 |
CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
|
|
159 |
CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
|
|
160 |
CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
|
|
161 |
CGContextClosePath(context);
|
|
162 |
CGContextRestoreGState(context);
|
|
163 |
}
|
|
164 |
|
3573
|
165 |
-(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh {
|
|
166 |
CGFloat cornerWidth = sizewh.width;
|
|
167 |
CGFloat cornerHeight = sizewh.height;
|
|
168 |
CGFloat w = self.size.width;
|
|
169 |
CGFloat h = self.size.height;
|
3547
|
170 |
|
|
171 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
|
172 |
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
|
|
173 |
|
|
174 |
CGContextBeginPath(context);
|
|
175 |
CGRect rect = CGRectMake(0, 0, w, h);
|
|
176 |
addRoundedRectToPath(context, rect, cornerWidth, cornerHeight);
|
|
177 |
CGContextClosePath(context);
|
|
178 |
CGContextClip(context);
|
|
179 |
|
3573
|
180 |
CGContextDrawImage(context, CGRectMake(0, 0, w, h), [self CGImage]);
|
3547
|
181 |
|
|
182 |
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
|
|
183 |
CGContextRelease(context);
|
|
184 |
CGColorSpaceRelease(colorSpace);
|
|
185 |
|
3573
|
186 |
UIImage *newImage = [UIImage imageWithCGImage:imageMasked];
|
3547
|
187 |
CGImageRelease(imageMasked);
|
|
188 |
|
|
189 |
return newImage;
|
|
190 |
}
|
|
191 |
|
|
192 |
@end
|