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
|
|
34 |
UIGraphicsBeginImageContext(self.size);
|
|
35 |
|
|
36 |
// drav the background image
|
|
37 |
[self drawAtPoint:CGPointMake(0,0)];
|
|
38 |
|
|
39 |
// draw the image on top of the first image
|
|
40 |
[secondImage drawAtPoint:secondImagePoint];
|
|
41 |
|
|
42 |
// create an image from the current contex (not thread safe)
|
|
43 |
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
|
|
44 |
|
|
45 |
// free drawing contex
|
|
46 |
UIGraphicsEndImageContext();
|
|
47 |
|
|
48 |
// return the resulting autoreleased image
|
|
49 |
return resultImage;
|
|
50 |
}
|
|
51 |
|
|
52 |
-(id) initWithContentsOfFile:(NSString *)path andCutAt:(CGRect) rect {
|
|
53 |
// load image from path
|
|
54 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile: path];
|
|
55 |
|
|
56 |
if (nil != image) {
|
|
57 |
// get its CGImage representation with a give size
|
|
58 |
CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], rect);
|
|
59 |
|
|
60 |
// clean memory
|
|
61 |
[image release];
|
|
62 |
|
|
63 |
// create a UIImage from the CGImage (memory must be allocated already)
|
|
64 |
UIImage *sprite = [self initWithCGImage:cgImgage];
|
|
65 |
|
|
66 |
// clean memory
|
|
67 |
CGImageRelease(cgImgage);
|
|
68 |
|
|
69 |
// return resulting image
|
|
70 |
return sprite;
|
|
71 |
} else {
|
|
72 |
NSLog(@"initWithContentsOfFile: andCutAt: FAILED");
|
|
73 |
return nil;
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
@end
|