project_files/HedgewarsMobile/Classes/UIImageExtra.m
author koda
Sat, 25 Sep 2010 18:38:05 +0200
changeset 3903 db01c37494af
parent 3829 81db3c85784b
child 3910 dd47efbdec46
permissions -rw-r--r--
pimp-my-savagame-table
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3829
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
     1
/*
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
     2
 * Hedgewars-iOS, a Hedgewars port for iOS devices
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
     3
 * Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com>
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
     4
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
     8
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    12
 * GNU General Public License for more details.
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    13
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    14
 * You should have received a copy of the GNU General Public License
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    15
 * along with this program; if not, write to the Free Software
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    17
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    18
 * File created on 08/04/2010.
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    19
 */
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    20
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
#import "UIImageExtra.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
@implementation UIImage (extra)
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    26
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
-(UIImage *)scaleToSize:(CGSize) size {
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    28
    DLog(@"warning - this is a very expensive operation, you should avoid using it");
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    29
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    30
    // Create a bitmap graphics context; this will also set it as the current context
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    31
    UIGraphicsBeginImageContext(size);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    32
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    33
    // Draw the scaled image in the current context
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    34
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    35
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    36
    // Create a new image from current context
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    37
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    38
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    39
    // Pop the current context from the stack
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    40
    UIGraphicsEndImageContext();
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    41
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    42
    // Return our new scaled image (autoreleased)
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    43
    return scaledImage;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
-(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
    // create a contex of size of the background image
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
    return [self mergeWith:secondImage atPoint:secondImagePoint atSize:self.size];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
-(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint atSize:(CGSize) resultingSize {
3778
2e61bb50cc57 add a nice group of hogs in lobby page
koda
parents: 3697
diff changeset
    52
    if (secondImage == nil) {
2e61bb50cc57 add a nice group of hogs in lobby page
koda
parents: 3697
diff changeset
    53
        DLog(@"Warning, secondImage == nil");
2e61bb50cc57 add a nice group of hogs in lobby page
koda
parents: 3697
diff changeset
    54
        return self;
2e61bb50cc57 add a nice group of hogs in lobby page
koda
parents: 3697
diff changeset
    55
    }
2e61bb50cc57 add a nice group of hogs in lobby page
koda
parents: 3697
diff changeset
    56
    
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    57
    // Create a bitmap graphics context; this will also set it as the current context
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
    UIGraphicsBeginImageContext(resultingSize);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    59
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    60
    // draw the background image in the current context
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
    [self drawAtPoint:CGPointMake(0,0)];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    62
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    63
    // 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
    64
    [secondImage drawAtPoint:secondImagePoint];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    65
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
    // create an image from the current contex (not thread safe)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    68
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
    // free drawing contex
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
    UIGraphicsEndImageContext();
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    71
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
    // return the resulting autoreleased image
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
    return resultImage;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
-(id) initWithContentsOfFile:(NSString *)path andCutAt:(CGRect) rect {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
    // load image from path
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
    UIImage *image = [[UIImage alloc] initWithContentsOfFile: path];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    79
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    80
    if (nil != image) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
        // get its CGImage representation with a give size
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
    82
        CGImageRef cgImage = CGImageCreateWithImageInRect([image CGImage], rect);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    83
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
        // clean memory
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
        [image release];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    86
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    87
        // create a UIImage from the CGImage (memory must be allocated already)
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
    88
        UIImage *sprite = [self initWithCGImage:cgImage];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    89
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    90
        // clean memory
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
    91
        CGImageRelease(cgImage);
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
        // return resulting image
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
        return sprite;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
        DLog(@"error - image == nil");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
        return nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    98
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   101
-(UIImage *)cutAt:(CGRect) rect {
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   102
    CGImageRef cgImage = CGImageCreateWithImageInRect([self CGImage], rect);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   103
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   104
    UIImage *res = [UIImage imageWithCGImage:cgImage];
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   105
    CGImageRelease(cgImage);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   106
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   107
    return res;
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   108
}
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   109
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   110
-(UIImage *)convertToGrayScale {
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   111
    // Create image rectangle with current image width/height
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   112
    CGRect imageRect = CGRectMake(0, 0, self.size.width, self.size.height);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   113
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   114
    // Grayscale color space
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   115
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   116
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   117
    // Create bitmap content with current image size and grayscale colorspace
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   118
    CGContextRef context = CGBitmapContextCreate(nil, self.size.width, self.size.height, 8, 0, colorSpace, kCGImageAlphaNone);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   119
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   120
    // Draw image into current context, with specified rectangle
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   121
    // using previously defined context (with grayscale colorspace)
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   122
    CGContextDrawImage(context, imageRect, [self CGImage]);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   123
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   124
    // Create bitmap image info from pixel data in current context
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   125
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   126
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   127
    // Create a new UIImage object
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   128
    UIImage *newImage = [UIImage imageWithCGImage:imageRef];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   129
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   130
    // Release colorspace, context and bitmap information
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   131
    CGColorSpaceRelease(colorSpace);
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   132
    CGContextRelease(context);
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   133
    CFRelease(imageRef);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   134
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   135
    // Return the new grayscale image
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   136
    return newImage;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   137
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   138
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
// 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
   140
-(UIImage*) maskImageWith:(UIImage *)maskImage {
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   141
    // prepare the reference image
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   142
    CGImageRef maskRef = [maskImage CGImage];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   143
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   144
    // create the mask using parameters of the mask reference
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145
    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   146
                                        CGImageGetHeight(maskRef),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
                                        CGImageGetBitsPerComponent(maskRef),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   148
                                        CGImageGetBitsPerPixel(maskRef),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
                                        CGImageGetBytesPerRow(maskRef),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
                                        CGImageGetDataProvider(maskRef), NULL, false);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   151
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   152
    // create an image in the current context
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
    CGImageRef masked = CGImageCreateWithMask([self CGImage], mask);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
    CGImageRelease(mask);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   155
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
    UIImage* retImage = [UIImage imageWithCGImage:masked];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
    CGImageRelease(masked);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   158
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
    return retImage;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   161
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   162
// 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
   163
void addRoundedRectToPath(CGContextRef context, CGRect rect, CGFloat ovalWidth, CGFloat ovalHeight) {
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   164
    CGFloat fw, fh;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   165
    if (ovalWidth == 0 || ovalHeight == 0) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   166
        CGContextAddRect(context, rect);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   167
        return;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   168
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   169
    CGContextSaveGState(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   170
    CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   171
    CGContextScaleCTM (context, ovalWidth, ovalHeight);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   172
    fw = CGRectGetWidth (rect) / ovalWidth;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   173
    fh = CGRectGetHeight (rect) / ovalHeight;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   174
    CGContextMoveToPoint(context, fw, fh/2);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   175
    CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   176
    CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   177
    CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   178
    CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   179
    CGContextClosePath(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   180
    CGContextRestoreGState(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   181
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   182
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   183
-(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh {
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   184
    CGFloat cornerWidth = sizewh.width;
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   185
    CGFloat cornerHeight = sizewh.height;
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   186
    CGFloat w = self.size.width;
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   187
    CGFloat h = self.size.height;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   188
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   189
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   190
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   191
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   192
    CGContextBeginPath(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   193
    CGRect rect = CGRectMake(0, 0, w, h);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   194
    addRoundedRectToPath(context, rect, cornerWidth, cornerHeight);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   195
    CGContextClosePath(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   196
    CGContextClip(context);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   197
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   198
    CGContextDrawImage(context, CGRectMake(0, 0, w, h), [self CGImage]);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   199
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   200
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   201
    CGContextRelease(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   202
    CGColorSpaceRelease(colorSpace);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   203
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   204
    UIImage *newImage = [UIImage imageWithCGImage:imageMasked];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   205
    CGImageRelease(imageMasked);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   206
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   207
    return newImage;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   208
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   209
3903
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   210
// by http://www.sixtemia.com/journal/2010/06/23/uiimage-negative-color-effect/
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   211
-(UIImage *)convertToNegative {
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   212
    UIGraphicsBeginImageContext(self.size);
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   213
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   214
    [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   215
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference);
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   216
    CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor);
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   217
    CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, self.size.width, self.size.height));
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   218
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   219
    UIGraphicsEndImageContext();
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   220
    return result;
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   221
}
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   222
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   223
@end