project_files/HedgewarsMobile/Classes/UIImageExtra.m
changeset 3697 d5b30d6373fc
parent 3621 a8ddf681ba7d
child 3778 2e61bb50cc57
equal deleted inserted replaced
3695:c11abf387a7d 3697:d5b30d6373fc
     8 
     8 
     9 #import "UIImageExtra.h"
     9 #import "UIImageExtra.h"
    10 
    10 
    11 
    11 
    12 @implementation UIImage (extra)
    12 @implementation UIImage (extra)
    13  
    13 
    14 -(UIImage *)scaleToSize:(CGSize) size {
    14 -(UIImage *)scaleToSize:(CGSize) size {
    15     DLog(@"warning - this is a very expensive operation, you should avoid using it");
    15     DLog(@"warning - this is a very expensive operation, you should avoid using it");
    16     
    16 
    17     // Create a bitmap graphics context; this will also set it as the current context
    17     // Create a bitmap graphics context; this will also set it as the current context
    18     UIGraphicsBeginImageContext(size);
    18     UIGraphicsBeginImageContext(size);
    19     
    19 
    20     // Draw the scaled image in the current context
    20     // Draw the scaled image in the current context
    21     [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    21     [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    22     
    22 
    23     // Create a new image from current context
    23     // Create a new image from current context
    24     UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    24     UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    25     
    25 
    26     // Pop the current context from the stack
    26     // Pop the current context from the stack
    27     UIGraphicsEndImageContext();
    27     UIGraphicsEndImageContext();
    28     
    28 
    29     // Return our new scaled image (autoreleased)
    29     // Return our new scaled image (autoreleased)
    30     return scaledImage;
    30     return scaledImage;
    31 }
    31 }
    32 
    32 
    33 -(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint {
    33 -(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint {
    36 }
    36 }
    37 
    37 
    38 -(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint atSize:(CGSize) resultingSize {
    38 -(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint atSize:(CGSize) resultingSize {
    39     // Create a bitmap graphics context; this will also set it as the current context
    39     // Create a bitmap graphics context; this will also set it as the current context
    40     UIGraphicsBeginImageContext(resultingSize);
    40     UIGraphicsBeginImageContext(resultingSize);
    41     
    41 
    42     // draw the background image in the current context
    42     // draw the background image in the current context
    43     [self drawAtPoint:CGPointMake(0,0)];
    43     [self drawAtPoint:CGPointMake(0,0)];
    44     
    44 
    45     // draw the image on top of the first image (because the context is the same)
    45     // draw the image on top of the first image (because the context is the same)
    46     [secondImage drawAtPoint:secondImagePoint];
    46     [secondImage drawAtPoint:secondImagePoint];
    47     
    47 
    48     // create an image from the current contex (not thread safe)
    48     // create an image from the current contex (not thread safe)
    49     UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    49     UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    50     
    50 
    51     // free drawing contex
    51     // free drawing contex
    52     UIGraphicsEndImageContext();
    52     UIGraphicsEndImageContext();
    53     
    53 
    54     // return the resulting autoreleased image
    54     // return the resulting autoreleased image
    55     return resultImage;
    55     return resultImage;
    56 }
    56 }
    57 
    57 
    58 -(id) initWithContentsOfFile:(NSString *)path andCutAt:(CGRect) rect {
    58 -(id) initWithContentsOfFile:(NSString *)path andCutAt:(CGRect) rect {
    59     // load image from path
    59     // load image from path
    60     UIImage *image = [[UIImage alloc] initWithContentsOfFile: path];
    60     UIImage *image = [[UIImage alloc] initWithContentsOfFile: path];
    61     
    61 
    62     if (nil != image) {
    62     if (nil != image) {
    63         // get its CGImage representation with a give size
    63         // get its CGImage representation with a give size
    64         CGImageRef cgImage = CGImageCreateWithImageInRect([image CGImage], rect);
    64         CGImageRef cgImage = CGImageCreateWithImageInRect([image CGImage], rect);
    65     
    65 
    66         // clean memory
    66         // clean memory
    67         [image release];
    67         [image release];
    68     
    68 
    69         // create a UIImage from the CGImage (memory must be allocated already)
    69         // create a UIImage from the CGImage (memory must be allocated already)
    70         UIImage *sprite = [self initWithCGImage:cgImage];
    70         UIImage *sprite = [self initWithCGImage:cgImage];
    71     
    71 
    72         // clean memory
    72         // clean memory
    73         CGImageRelease(cgImage);
    73         CGImageRelease(cgImage);
    74 
    74 
    75         // return resulting image
    75         // return resulting image
    76         return sprite;
    76         return sprite;
    80     }
    80     }
    81 }
    81 }
    82 
    82 
    83 -(UIImage *)cutAt:(CGRect) rect {
    83 -(UIImage *)cutAt:(CGRect) rect {
    84     CGImageRef cgImage = CGImageCreateWithImageInRect([self CGImage], rect);
    84     CGImageRef cgImage = CGImageCreateWithImageInRect([self CGImage], rect);
    85     
    85 
    86     UIImage *res = [UIImage imageWithCGImage:cgImage];
    86     UIImage *res = [UIImage imageWithCGImage:cgImage];
    87     CGImageRelease(cgImage);
    87     CGImageRelease(cgImage);
    88     
    88 
    89     return res;
    89     return res;
    90 }
    90 }
    91 
    91 
    92 -(UIImage *)convertToGrayScale {
    92 -(UIImage *)convertToGrayScale {
    93     // Create image rectangle with current image width/height
    93     // Create image rectangle with current image width/height
    94     CGRect imageRect = CGRectMake(0, 0, self.size.width, self.size.height);
    94     CGRect imageRect = CGRectMake(0, 0, self.size.width, self.size.height);
    95     
    95 
    96     // Grayscale color space
    96     // Grayscale color space
    97     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    97     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    98     
    98 
    99     // Create bitmap content with current image size and grayscale colorspace
    99     // Create bitmap content with current image size and grayscale colorspace
   100     CGContextRef context = CGBitmapContextCreate(nil, self.size.width, self.size.height, 8, 0, colorSpace, kCGImageAlphaNone);
   100     CGContextRef context = CGBitmapContextCreate(nil, self.size.width, self.size.height, 8, 0, colorSpace, kCGImageAlphaNone);
   101     
   101 
   102     // Draw image into current context, with specified rectangle
   102     // Draw image into current context, with specified rectangle
   103     // using previously defined context (with grayscale colorspace)
   103     // using previously defined context (with grayscale colorspace)
   104     CGContextDrawImage(context, imageRect, [self CGImage]);
   104     CGContextDrawImage(context, imageRect, [self CGImage]);
   105     
   105 
   106     // Create bitmap image info from pixel data in current context
   106     // Create bitmap image info from pixel data in current context
   107     CGImageRef imageRef = CGBitmapContextCreateImage(context);
   107     CGImageRef imageRef = CGBitmapContextCreateImage(context);
   108     
   108 
   109     // Create a new UIImage object  
   109     // Create a new UIImage object
   110     UIImage *newImage = [UIImage imageWithCGImage:imageRef];
   110     UIImage *newImage = [UIImage imageWithCGImage:imageRef];
   111     
   111 
   112     // Release colorspace, context and bitmap information
   112     // Release colorspace, context and bitmap information
   113     CGColorSpaceRelease(colorSpace);
   113     CGColorSpaceRelease(colorSpace);
   114     CGContextRelease(context);
   114     CGContextRelease(context);
   115     CFRelease(imageRef);
   115     CFRelease(imageRef);
   116     
   116 
   117     // Return the new grayscale image
   117     // Return the new grayscale image
   118     return newImage;
   118     return newImage;
   119 }
   119 }
   120 
   120 
   121 // by http://iphonedevelopertips.com/cocoa/how-to-mask-an-image.html turned into a category by koda
   121 // by http://iphonedevelopertips.com/cocoa/how-to-mask-an-image.html turned into a category by koda
   122 -(UIImage*) maskImageWith:(UIImage *)maskImage {
   122 -(UIImage*) maskImageWith:(UIImage *)maskImage {
   123     // prepare the reference image
   123     // prepare the reference image
   124     CGImageRef maskRef = [maskImage CGImage];
   124     CGImageRef maskRef = [maskImage CGImage];
   125     
   125 
   126     // create the mask using parameters of the mask reference
   126     // create the mask using parameters of the mask reference
   127     CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
   127     CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
   128                                         CGImageGetHeight(maskRef),
   128                                         CGImageGetHeight(maskRef),
   129                                         CGImageGetBitsPerComponent(maskRef),
   129                                         CGImageGetBitsPerComponent(maskRef),
   130                                         CGImageGetBitsPerPixel(maskRef),
   130                                         CGImageGetBitsPerPixel(maskRef),
   131                                         CGImageGetBytesPerRow(maskRef),
   131                                         CGImageGetBytesPerRow(maskRef),
   132                                         CGImageGetDataProvider(maskRef), NULL, false);
   132                                         CGImageGetDataProvider(maskRef), NULL, false);
   133     
   133 
   134     // create an image in the current context
   134     // create an image in the current context
   135     CGImageRef masked = CGImageCreateWithMask([self CGImage], mask);
   135     CGImageRef masked = CGImageCreateWithMask([self CGImage], mask);
   136     CGImageRelease(mask);
   136     CGImageRelease(mask);
   137     
   137 
   138     UIImage* retImage = [UIImage imageWithCGImage:masked];
   138     UIImage* retImage = [UIImage imageWithCGImage:masked];
   139     CGImageRelease(masked);
   139     CGImageRelease(masked);
   140     
   140 
   141     return retImage;
   141     return retImage;
   142 }
   142 }
   143 
   143 
   144 // by http://blog.sallarp.com/iphone-uiimage-round-corners/ turned into a category by koda
   144 // by http://blog.sallarp.com/iphone-uiimage-round-corners/ turned into a category by koda
   145 void addRoundedRectToPath(CGContextRef context, CGRect rect, CGFloat ovalWidth, CGFloat ovalHeight) {
   145 void addRoundedRectToPath(CGContextRef context, CGRect rect, CGFloat ovalWidth, CGFloat ovalHeight) {
   160     CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
   160     CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
   161     CGContextClosePath(context);
   161     CGContextClosePath(context);
   162     CGContextRestoreGState(context);
   162     CGContextRestoreGState(context);
   163 }
   163 }
   164 
   164 
   165 -(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh {    
   165 -(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh {
   166     CGFloat cornerWidth = sizewh.width;
   166     CGFloat cornerWidth = sizewh.width;
   167     CGFloat cornerHeight = sizewh.height;
   167     CGFloat cornerHeight = sizewh.height;
   168     CGFloat w = self.size.width;
   168     CGFloat w = self.size.width;
   169     CGFloat h = self.size.height;
   169     CGFloat h = self.size.height;
   170     
   170 
   171     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   171     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   172     CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
   172     CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
   173     
   173 
   174     CGContextBeginPath(context);
   174     CGContextBeginPath(context);
   175     CGRect rect = CGRectMake(0, 0, w, h);
   175     CGRect rect = CGRectMake(0, 0, w, h);
   176     addRoundedRectToPath(context, rect, cornerWidth, cornerHeight);
   176     addRoundedRectToPath(context, rect, cornerWidth, cornerHeight);
   177     CGContextClosePath(context);
   177     CGContextClosePath(context);
   178     CGContextClip(context);
   178     CGContextClip(context);
   179     
   179 
   180     CGContextDrawImage(context, CGRectMake(0, 0, w, h), [self CGImage]);
   180     CGContextDrawImage(context, CGRectMake(0, 0, w, h), [self CGImage]);
   181     
   181 
   182     CGImageRef imageMasked = CGBitmapContextCreateImage(context);
   182     CGImageRef imageMasked = CGBitmapContextCreateImage(context);
   183     CGContextRelease(context);
   183     CGContextRelease(context);
   184     CGColorSpaceRelease(colorSpace);
   184     CGColorSpaceRelease(colorSpace);
   185     
   185 
   186     UIImage *newImage = [UIImage imageWithCGImage:imageMasked];
   186     UIImage *newImage = [UIImage imageWithCGImage:imageMasked];
   187     CGImageRelease(imageMasked);
   187     CGImageRelease(imageMasked);
   188         
   188 
   189     return newImage;
   189     return newImage;
   190 }
   190 }
   191 
   191 
   192 @end
   192 @end