project_files/HedgewarsMobile/Classes/UIImageExtra.m
author koda
Sat, 10 Jul 2010 15:39:07 +0200
changeset 3634 93d260c96635
parent 3621 a8ddf681ba7d
child 3697 d5b30d6373fc
permissions -rw-r--r--
cWeaponTooltips -> rqTooltipsOff
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
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
    64
        CGImageRef cgImage = CGImageCreateWithImageInRect([image CGImage], rect);
3547
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)
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
    70
        UIImage *sprite = [self initWithCGImage:cgImage];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
        // clean memory
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
    73
        CGImageRelease(cgImage);
3547
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
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
    83
-(UIImage *)cutAt:(CGRect) rect {
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
    84
    CGImageRef cgImage = CGImageCreateWithImageInRect([self CGImage], rect);
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
    85
    
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
    86
    UIImage *res = [UIImage imageWithCGImage:cgImage];
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
    87
    CGImageRelease(cgImage);
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
    88
    
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
    89
    return res;
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
    90
}
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
    91
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
-(UIImage *)convertToGrayScale {
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    93
    // Create image rectangle with current image width/height
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    94
    CGRect imageRect = CGRectMake(0, 0, self.size.width, self.size.height);
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    95
    
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    96
    // Grayscale color space
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    97
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    98
    
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    99
    // Create bitmap content with current image size and grayscale colorspace
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   100
    CGContextRef context = CGBitmapContextCreate(nil, self.size.width, self.size.height, 8, 0, colorSpace, kCGImageAlphaNone);
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   101
    
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   102
    // Draw image into current context, with specified rectangle
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   103
    // using previously defined context (with grayscale colorspace)
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   104
    CGContextDrawImage(context, imageRect, [self CGImage]);
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   105
    
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   106
    // Create bitmap image info from pixel data in current context
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   107
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   108
    
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   109
    // Create a new UIImage object  
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   110
    UIImage *newImage = [UIImage imageWithCGImage:imageRef];
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   111
    
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   112
    // Release colorspace, context and bitmap information
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   113
    CGColorSpaceRelease(colorSpace);
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   114
    CGContextRelease(context);
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   115
    CFRelease(imageRef);
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   116
    
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   117
    // Return the new grayscale image
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   118
    return newImage;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   121
// 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
   122
-(UIImage*) maskImageWith:(UIImage *)maskImage {
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   123
    // prepare the reference image
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   124
    CGImageRef maskRef = [maskImage CGImage];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   125
    
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   126
    // create the mask using parameters of the mask reference
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   127
    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   128
                                        CGImageGetHeight(maskRef),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
                                        CGImageGetBitsPerComponent(maskRef),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
                                        CGImageGetBitsPerPixel(maskRef),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   131
                                        CGImageGetBytesPerRow(maskRef),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   132
                                        CGImageGetDataProvider(maskRef), NULL, false);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   133
    
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   134
    // create an image in the current context
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
    CGImageRef masked = CGImageCreateWithMask([self CGImage], mask);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   136
    CGImageRelease(mask);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   137
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   138
    UIImage* retImage = [UIImage imageWithCGImage:masked];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
    CGImageRelease(masked);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
    return retImage;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   142
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
// 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
   145
void addRoundedRectToPath(CGContextRef context, CGRect rect, CGFloat ovalWidth, CGFloat ovalHeight) {
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   146
    CGFloat fw, fh;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
    if (ovalWidth == 0 || ovalHeight == 0) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   148
        CGContextAddRect(context, rect);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
        return;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
    CGContextSaveGState(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
    CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
    CGContextScaleCTM (context, ovalWidth, ovalHeight);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
    fw = CGRectGetWidth (rect) / ovalWidth;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
    fh = CGRectGetHeight (rect) / ovalHeight;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
    CGContextMoveToPoint(context, fw, fh/2);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
    CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
    CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
    CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160
    CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   161
    CGContextClosePath(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   162
    CGContextRestoreGState(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   163
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   164
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   165
-(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh {    
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   166
    CGFloat cornerWidth = sizewh.width;
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   167
    CGFloat cornerHeight = sizewh.height;
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   168
    CGFloat w = self.size.width;
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   169
    CGFloat h = self.size.height;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   170
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   171
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   172
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   173
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   174
    CGContextBeginPath(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   175
    CGRect rect = CGRectMake(0, 0, w, h);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   176
    addRoundedRectToPath(context, rect, cornerWidth, cornerHeight);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   177
    CGContextClosePath(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   178
    CGContextClip(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   179
    
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   180
    CGContextDrawImage(context, CGRectMake(0, 0, w, h), [self CGImage]);
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   181
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   182
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   183
    CGContextRelease(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   184
    CGColorSpaceRelease(colorSpace);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   185
    
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   186
    UIImage *newImage = [UIImage imageWithCGImage:imageMasked];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   187
    CGImageRelease(imageMasked);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   188
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   189
    return newImage;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   190
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   191
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   192
@end