3352
|
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 {
|
|
15 |
// Create a bitmap graphics context
|
|
16 |
// This will also set it as the current context
|
|
17 |
UIGraphicsBeginImageContext(size);
|
|
18 |
|
|
19 |
// Draw the scaled image in the current context
|
|
20 |
[self drawInRect:CGRectMake(0, 0, size.width, size.height)];
|
|
21 |
|
|
22 |
// Create a new image from current context
|
|
23 |
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
|
|
24 |
|
|
25 |
// Pop the current context from the stack
|
|
26 |
UIGraphicsEndImageContext();
|
|
27 |
|
|
28 |
// Return our new scaled image (autoreleased)
|
|
29 |
return scaledImage;
|
|
30 |
}
|
|
31 |
|
|
32 |
-(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint {
|
|
33 |
// create a contex of size of the background image
|
3361
|
34 |
return [self mergeWith:secondImage atPoint:secondImagePoint atSize:self.size];
|
|
35 |
}
|
|
36 |
|
|
37 |
-(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint atSize:(CGSize) resultingSize {
|
|
38 |
UIGraphicsBeginImageContext(resultingSize);
|
3352
|
39 |
|
|
40 |
// drav the background image
|
|
41 |
[self drawAtPoint:CGPointMake(0,0)];
|
|
42 |
|
|
43 |
// draw the image on top of the first image
|
|
44 |
[secondImage drawAtPoint:secondImagePoint];
|
|
45 |
|
|
46 |
// create an image from the current contex (not thread safe)
|
|
47 |
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
|
|
48 |
|
|
49 |
// free drawing contex
|
|
50 |
UIGraphicsEndImageContext();
|
|
51 |
|
|
52 |
// return the resulting autoreleased image
|
|
53 |
return resultImage;
|
|
54 |
}
|
|
55 |
|
|
56 |
-(id) initWithContentsOfFile:(NSString *)path andCutAt:(CGRect) rect {
|
|
57 |
// load image from path
|
|
58 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile: path];
|
|
59 |
|
|
60 |
if (nil != image) {
|
|
61 |
// get its CGImage representation with a give size
|
|
62 |
CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], rect);
|
|
63 |
|
|
64 |
// clean memory
|
|
65 |
[image release];
|
|
66 |
|
|
67 |
// create a UIImage from the CGImage (memory must be allocated already)
|
|
68 |
UIImage *sprite = [self initWithCGImage:cgImgage];
|
|
69 |
|
|
70 |
// clean memory
|
|
71 |
CGImageRelease(cgImgage);
|
|
72 |
|
|
73 |
// return resulting image
|
|
74 |
return sprite;
|
|
75 |
} else {
|
|
76 |
NSLog(@"initWithContentsOfFile: andCutAt: FAILED");
|
|
77 |
return nil;
|
|
78 |
}
|
|
79 |
}
|
3463
|
80 |
|
|
81 |
-(UIImage *)convertImageToGrayScale:(UIImage *)image {
|
|
82 |
// Create image rectangle with current image width/height
|
|
83 |
CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
|
|
84 |
|
|
85 |
// Grayscale color space
|
|
86 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
|
|
87 |
|
|
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);
|
|
90 |
|
|
91 |
// Draw image into current context, with specified rectangle
|
|
92 |
// using previously defined context (with grayscale colorspace)
|
|
93 |
CGContextDrawImage(context, imageRect, [image CGImage]);
|
|
94 |
|
|
95 |
// Create bitmap image info from pixel data in current context
|
|
96 |
CGImageRef imageRef = CGBitmapContextCreateImage(context);
|
|
97 |
|
|
98 |
// Create a new UIImage object
|
|
99 |
UIImage *newImage = [UIImage imageWithCGImage:imageRef];
|
|
100 |
|
|
101 |
// Release colorspace, context and bitmap information
|
|
102 |
CGColorSpaceRelease(colorSpace);
|
|
103 |
CGContextRelease(context);
|
|
104 |
CFRelease(imageRef);
|
|
105 |
|
|
106 |
// Return the new grayscale image
|
|
107 |
return newImage;
|
|
108 |
}
|
3352
|
109 |
|
|
110 |
@end
|