project_files/HedgewarsMobile/Classes/UIImageExtra.m
changeset 6209 074ab6ebcb3e
parent 6080 ce02ddfe8aa1
child 6700 e04da46ee43c
equal deleted inserted replaced
6208:b831679e9467 6209:074ab6ebcb3e
    23 
    23 
    24 
    24 
    25 @implementation UIImage (extra)
    25 @implementation UIImage (extra)
    26 
    26 
    27 -(UIImage *)scaleToSize:(CGSize) size {
    27 -(UIImage *)scaleToSize:(CGSize) size {
    28     DLog(@"warning - this is a very expensive operation, you should avoid using it");
       
    29 
       
    30     // Create a bitmap graphics context; this will also set it as the current context
    28     // Create a bitmap graphics context; this will also set it as the current context
    31     UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);
    29     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    32 
    30     CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, 8, 4 * size.width, colorSpace, kCGImageAlphaPremultipliedFirst);
    33     // Draw the scaled image in the current context
    31 
    34     [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    32     // draw the image inside the context
    35 
    33     CGFloat screenScale = [[UIScreen mainScreen] safeScale];
    36     // Create a new image from current context
    34     CGContextDrawImage(context, CGRectMake(0, 0, size.width*screenScale, size.height*screenScale), self.CGImage);
    37     UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    35 
    38 
    36     // Create bitmap image info from pixel data in current context
    39     // Pop the current context from the stack
    37     CGImageRef imageRef = CGBitmapContextCreateImage(context);
    40     UIGraphicsEndImageContext();
    38 
    41 
    39     // Create a new UIImage object
    42     // Return our new scaled image (autoreleased)
    40     UIImage *resultImage;
    43     return scaledImage;
    41     if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
       
    42         resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp];
       
    43     else
       
    44         resultImage = [UIImage imageWithCGImage:imageRef];
       
    45 
       
    46     // Release colorspace, context and bitmap information
       
    47     CGColorSpaceRelease(colorSpace);
       
    48     CGContextRelease(context);
       
    49     CFRelease(imageRef);
       
    50 
       
    51     return resultImage;
    44 }
    52 }
    45 
    53 
    46 -(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint {
    54 -(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint {
    47     if (secondImage == nil) {
    55     if (secondImage == nil) {
    48         DLog(@"Warning, secondImage == nil");
    56         DLog(@"Warning, secondImage == nil");
    49         return self;
    57         return self;
    50     }
    58     }
    51     CGFloat screenScale = [[UIScreen mainScreen] scale];
    59     CGFloat screenScale = [[UIScreen mainScreen] safeScale];
    52     int w = self.size.width * screenScale;
    60     int w = self.size.width * screenScale;
    53     int h = self.size.height * screenScale;
    61     int h = self.size.height * screenScale;
    54     int yOffset = self.size.height - secondImage.size.height + secondImagePoint.y;
    62     int yOffset = self.size.height - secondImage.size.height + secondImagePoint.y;
    55 
    63 
    56     if (w == 0 || h == 0) {
    64     if (w == 0 || h == 0) {
    57         DLog(@"Cannot have 0 dimesions");
    65         DLog(@"Cannot have 0 dimesions");
    58         return self;
    66         return self;
    59     }
    67     }
    60     
    68 
    61     // Create a bitmap graphics context; this will also set it as the current context
    69     // Create a bitmap graphics context; this will also set it as the current context
    62     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    70     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    63     CGContextRef context = CGBitmapContextCreate(NULL, w, h+yOffset, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
    71     CGContextRef context = CGBitmapContextCreate(NULL, w, h+yOffset, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
    64     
    72 
    65     // draw the two images in the current context
    73     // draw the two images in the current context
    66     CGContextDrawImage(context, CGRectMake(0, 0, self.size.width*screenScale, self.size.height*screenScale), [self CGImage]);
    74     CGContextDrawImage(context, CGRectMake(0, 0, self.size.width*screenScale, self.size.height*screenScale), [self CGImage]);
    67     CGContextDrawImage(context, CGRectMake(secondImagePoint.x*screenScale, secondImagePoint.y*screenScale, secondImage.size.width*screenScale, secondImage.size.height*screenScale), [secondImage CGImage]);
    75     CGContextDrawImage(context, CGRectMake(secondImagePoint.x*screenScale, secondImagePoint.y*screenScale, secondImage.size.width*screenScale, secondImage.size.height*screenScale), [secondImage CGImage]);
    68     
    76 
    69     // Create bitmap image info from pixel data in current context
    77     // Create bitmap image info from pixel data in current context
    70     CGImageRef imageRef = CGBitmapContextCreateImage(context);
    78     CGImageRef imageRef = CGBitmapContextCreateImage(context);
    71     
    79 
    72     // Create a new UIImage object
    80     // Create a new UIImage object
    73     UIImage *resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp];
    81     UIImage *resultImage;
       
    82     if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
       
    83         resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp];
       
    84     else
       
    85         resultImage = [UIImage imageWithCGImage:imageRef];
    74 
    86 
    75     // Release colorspace, context and bitmap information
    87     // Release colorspace, context and bitmap information
    76     CGColorSpaceRelease(colorSpace);
    88     CGColorSpaceRelease(colorSpace);
    77     CGContextRelease(context);
    89     CGContextRelease(context);
    78     CFRelease(imageRef);
    90     CFRelease(imageRef);
    79    
    91 
    80     return resultImage;
    92     return resultImage;
    81 }
    93 }
    82 
    94 
    83 -(id) initWithContentsOfFile:(NSString *)path andCutAt:(CGRect) rect {
    95 -(id) initWithContentsOfFile:(NSString *)path andCutAt:(CGRect) rect {
    84     // load image from path
    96     // load image from path
   188 }
   200 }
   189 
   201 
   190 -(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh {
   202 -(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh {
   191     CGFloat cornerWidth = sizewh.width;
   203     CGFloat cornerWidth = sizewh.width;
   192     CGFloat cornerHeight = sizewh.height;
   204     CGFloat cornerHeight = sizewh.height;
   193     CGFloat screenScale = [[UIScreen mainScreen] scale];
   205     CGFloat screenScale = [[UIScreen mainScreen] safeScale];
   194     CGFloat w = self.size.width * screenScale;
   206     CGFloat w = self.size.width * screenScale;
   195     CGFloat h = self.size.height * screenScale;
   207     CGFloat h = self.size.height * screenScale;
   196 
   208 
   197     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   209     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   198     CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
   210     CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
   207 
   219 
   208     CGImageRef imageMasked = CGBitmapContextCreateImage(context);
   220     CGImageRef imageMasked = CGBitmapContextCreateImage(context);
   209     CGContextRelease(context);
   221     CGContextRelease(context);
   210     CGColorSpaceRelease(colorSpace);
   222     CGColorSpaceRelease(colorSpace);
   211 
   223 
   212     UIImage *newImage = [UIImage imageWithCGImage:imageMasked scale:screenScale orientation:UIImageOrientationUp];
   224     UIImage *resultImage;
   213 
   225     if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
       
   226         resultImage = [UIImage imageWithCGImage:imageMasked scale:screenScale orientation:UIImageOrientationUp];
       
   227     else
       
   228         resultImage = [UIImage imageWithCGImage:imageMasked];
   214     CGImageRelease(imageMasked);
   229     CGImageRelease(imageMasked);
   215 
   230 
   216     return newImage;
   231     return resultImage;
   217 }
   232 }
   218 
   233 
   219 // by http://www.sixtemia.com/journal/2010/06/23/uiimage-negative-color-effect/
   234 // by http://www.sixtemia.com/journal/2010/06/23/uiimage-negative-color-effect/
   220 -(UIImage *)convertToNegative {
   235 -(UIImage *)convertToNegative {
   221     UIGraphicsBeginImageContext(self.size);
   236     UIGraphicsBeginImageContext(self.size);
   249     UIImage *bkgImg = [UIImage imageWithCGImage:image];
   264     UIImage *bkgImg = [UIImage imageWithCGImage:image];
   250     CGImageRelease(image);
   265     CGImageRelease(image);
   251     return bkgImg;
   266     return bkgImg;
   252 }
   267 }
   253 
   268 
       
   269 +(UIImage *)drawHogsRepeated:(NSInteger) manyTimes {
       
   270     NSString *imgString = [[NSString alloc] initWithFormat:@"%@/hedgehog.png",[[NSBundle mainBundle] resourcePath]];
       
   271     UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:imgString];
       
   272     [imgString release];
       
   273     CGFloat screenScale = [[UIScreen mainScreen] safeScale];
       
   274     int w = hogSprite.size.width * screenScale;
       
   275     int h = hogSprite.size.height * screenScale;
       
   276     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
       
   277     CGContextRef context = CGBitmapContextCreate(NULL, w * 3, h, 8, 4 * w * 3, colorSpace, kCGImageAlphaPremultipliedFirst);
       
   278 
       
   279     // draw the two images in the current context
       
   280     for (int i = 0; i < manyTimes; i++)
       
   281         CGContextDrawImage(context, CGRectMake(i*8*screenScale, 0, w, h), [hogSprite CGImage]);
       
   282     [hogSprite release];
       
   283 
       
   284     // Create bitmap image info from pixel data in current context
       
   285     CGImageRef imageRef = CGBitmapContextCreateImage(context);
       
   286 
       
   287     // Create a new UIImage object
       
   288     UIImage *resultImage;
       
   289     if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
       
   290         resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp];
       
   291     else
       
   292         resultImage = [UIImage imageWithCGImage:imageRef];
       
   293 
       
   294     // Release colorspace, context and bitmap information
       
   295     CGColorSpaceRelease(colorSpace);
       
   296     CGContextRelease(context);
       
   297     CFRelease(imageRef);
       
   298 
       
   299     return resultImage;
       
   300 }
       
   301 
   254 // this routine checks for the PNG size without loading it in memory
   302 // this routine checks for the PNG size without loading it in memory
   255 // https://github.com/steipete/PSFramework/blob/master/PSFramework%20Version%200.3/PhotoshopFramework/PSMetaDataFunctions.m
   303 // https://github.com/steipete/PSFramework/blob/master/PSFramework%20Version%200.3/PhotoshopFramework/PSMetaDataFunctions.m
   256 +(CGSize) imageSizeFromMetadataOf:(NSString *)aFileName {
   304 +(CGSize) imageSizeFromMetadataOf:(NSString *)aFileName {
   257     // File Name to C String.
   305     // File Name to C String.
   258     const char *fileName = [aFileName UTF8String];
   306     const char *fileName = [aFileName UTF8String];