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