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