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 {
|
3573
|
39 |
// Create a bitmap graphics context; this will also set it as the current context
|
3547
|
40 |
UIGraphicsBeginImageContext(resultingSize);
|
3697
|
41 |
|
3573
|
42 |
// draw the background image in the current context
|
3547
|
43 |
[self drawAtPoint:CGPointMake(0,0)];
|
3697
|
44 |
|
3573
|
45 |
// draw the image on top of the first image (because the context is the same)
|
3547
|
46 |
[secondImage drawAtPoint:secondImagePoint];
|
3697
|
47 |
|
3547
|
48 |
// create an image from the current contex (not thread safe)
|
|
49 |
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
|
3697
|
50 |
|
3547
|
51 |
// free drawing contex
|
|
52 |
UIGraphicsEndImageContext();
|
3697
|
53 |
|
3547
|
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];
|
3697
|
61 |
|
3547
|
62 |
if (nil != image) {
|
|
63 |
// get its CGImage representation with a give size
|
3621
|
64 |
CGImageRef cgImage = CGImageCreateWithImageInRect([image CGImage], rect);
|
3697
|
65 |
|
3547
|
66 |
// clean memory
|
|
67 |
[image release];
|
3697
|
68 |
|
3547
|
69 |
// create a UIImage from the CGImage (memory must be allocated already)
|
3621
|
70 |
UIImage *sprite = [self initWithCGImage:cgImage];
|
3697
|
71 |
|
3547
|
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);
|
3697
|
85 |
|
3621
|
86 |
UIImage *res = [UIImage imageWithCGImage:cgImage];
|
|
87 |
CGImageRelease(cgImage);
|
3697
|
88 |
|
3621
|
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);
|
3697
|
95 |
|
3573
|
96 |
// Grayscale color space
|
|
97 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
|
3697
|
98 |
|
3573
|
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);
|
3697
|
101 |
|
3573
|
102 |
// Draw image into current context, with specified rectangle
|
|
103 |
// using previously defined context (with grayscale colorspace)
|
|
104 |
CGContextDrawImage(context, imageRect, [self CGImage]);
|
3697
|
105 |
|
3573
|
106 |
// Create bitmap image info from pixel data in current context
|
|
107 |
CGImageRef imageRef = CGBitmapContextCreateImage(context);
|
3697
|
108 |
|
|
109 |
// Create a new UIImage object
|
3573
|
110 |
UIImage *newImage = [UIImage imageWithCGImage:imageRef];
|
3697
|
111 |
|
3573
|
112 |
// Release colorspace, context and bitmap information
|
|
113 |
CGColorSpaceRelease(colorSpace);
|
|
114 |
CGContextRelease(context);
|
|
115 |
CFRelease(imageRef);
|
3697
|
116 |
|
3573
|
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];
|
3697
|
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);
|
3697
|
133 |
|
3573
|
134 |
// create an image in the current context
|
3547
|
135 |
CGImageRef masked = CGImageCreateWithMask([self CGImage], mask);
|
|
136 |
CGImageRelease(mask);
|
3697
|
137 |
|
3547
|
138 |
UIImage* retImage = [UIImage imageWithCGImage:masked];
|
|
139 |
CGImageRelease(masked);
|
3697
|
140 |
|
3547
|
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 |
|
3697
|
165 |
-(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh {
|
3573
|
166 |
CGFloat cornerWidth = sizewh.width;
|
|
167 |
CGFloat cornerHeight = sizewh.height;
|
|
168 |
CGFloat w = self.size.width;
|
|
169 |
CGFloat h = self.size.height;
|
3697
|
170 |
|
3547
|
171 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
|
172 |
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
|
3697
|
173 |
|
3547
|
174 |
CGContextBeginPath(context);
|
|
175 |
CGRect rect = CGRectMake(0, 0, w, h);
|
|
176 |
addRoundedRectToPath(context, rect, cornerWidth, cornerHeight);
|
|
177 |
CGContextClosePath(context);
|
|
178 |
CGContextClip(context);
|
3697
|
179 |
|
3573
|
180 |
CGContextDrawImage(context, CGRectMake(0, 0, w, h), [self CGImage]);
|
3697
|
181 |
|
3547
|
182 |
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
|
|
183 |
CGContextRelease(context);
|
|
184 |
CGColorSpaceRelease(colorSpace);
|
3697
|
185 |
|
3573
|
186 |
UIImage *newImage = [UIImage imageWithCGImage:imageMasked];
|
3547
|
187 |
CGImageRelease(imageMasked);
|
3697
|
188 |
|
3547
|
189 |
return newImage;
|
|
190 |
}
|
|
191 |
|
|
192 |
@end
|