project_files/HedgewarsMobile/Classes/UIImageExtra.m
changeset 6078 8c0cc07731e5
parent 5503 d8632f589008
child 6080 ce02ddfe8aa1
equal deleted inserted replaced
6077:d8fa5a85d24f 6078:8c0cc07731e5
    22 #import "UIImageExtra.h"
    22 #import "UIImageExtra.h"
    23 
    23 
    24 
    24 
    25 @implementation UIImage (extra)
    25 @implementation UIImage (extra)
    26 
    26 
    27 CGFloat getScreenScale(void) {
       
    28     float scale = 1.0f;
       
    29     if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
       
    30         scale = [[UIScreen mainScreen] scale];
       
    31     return scale;
       
    32 }
       
    33 
       
    34 -(UIImage *)scaleToSize:(CGSize) size {
    27 -(UIImage *)scaleToSize:(CGSize) size {
    35     DLog(@"warning - this is a very expensive operation, you should avoid using it");
    28     DLog(@"warning - this is a very expensive operation, you should avoid using it");
    36 
    29 
    37     // Create a bitmap graphics context; this will also set it as the current context
    30     // Create a bitmap graphics context; this will also set it as the current context
    38     if (UIGraphicsBeginImageContextWithOptions != NULL)
    31     UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);
    39         UIGraphicsBeginImageContextWithOptions(size, NO, getScreenScale());
       
    40     else
       
    41         UIGraphicsBeginImageContext(size);
       
    42 
    32 
    43     // Draw the scaled image in the current context
    33     // Draw the scaled image in the current context
    44     [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    34     [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    45 
    35 
    46     // Create a new image from current context
    36     // Create a new image from current context
    56 -(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint {
    46 -(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint {
    57     if (secondImage == nil) {
    47     if (secondImage == nil) {
    58         DLog(@"Warning, secondImage == nil");
    48         DLog(@"Warning, secondImage == nil");
    59         return self;
    49         return self;
    60     }
    50     }
    61     CGFloat screenScale = getScreenScale();
    51     CGFloat screenScale = [[UIScreen mainScreen] scale];
    62     int w = self.size.width * screenScale;
    52     int w = self.size.width * screenScale;
    63     int h = self.size.height * screenScale;
    53     int h = self.size.height * screenScale;
    64     
    54     
    65     if (w == 0 || h == 0) {
    55     if (w == 0 || h == 0) {
    66         DLog(@"Can have 0 dimesions");
    56         DLog(@"Can have 0 dimesions");
    77     
    67     
    78     // Create bitmap image info from pixel data in current context
    68     // Create bitmap image info from pixel data in current context
    79     CGImageRef imageRef = CGBitmapContextCreateImage(context);
    69     CGImageRef imageRef = CGBitmapContextCreateImage(context);
    80     
    70     
    81     // Create a new UIImage object
    71     // Create a new UIImage object
    82     UIImage *resultImage;
    72     UIImage *resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp];
    83     if ([self respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
       
    84         resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp];
       
    85     else
       
    86         resultImage = [UIImage imageWithCGImage:imageRef];
       
    87 
    73 
    88     // Release colorspace, context and bitmap information
    74     // Release colorspace, context and bitmap information
    89     CGColorSpaceRelease(colorSpace);
    75     CGColorSpaceRelease(colorSpace);
    90     CGContextRelease(context);
    76     CGContextRelease(context);
    91     CFRelease(imageRef);
    77     CFRelease(imageRef);
   201 }
   187 }
   202 
   188 
   203 -(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh {
   189 -(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh {
   204     CGFloat cornerWidth = sizewh.width;
   190     CGFloat cornerWidth = sizewh.width;
   205     CGFloat cornerHeight = sizewh.height;
   191     CGFloat cornerHeight = sizewh.height;
   206     CGFloat screenScale = getScreenScale();
   192     CGFloat screenScale = [[UIScreen mainScreen] scale];
   207     CGFloat w = self.size.width * screenScale;
   193     CGFloat w = self.size.width * screenScale;
   208     CGFloat h = self.size.height * screenScale;
   194     CGFloat h = self.size.height * screenScale;
   209 
   195 
   210     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   196     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   211     CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
   197     CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
   220 
   206 
   221     CGImageRef imageMasked = CGBitmapContextCreateImage(context);
   207     CGImageRef imageMasked = CGBitmapContextCreateImage(context);
   222     CGContextRelease(context);
   208     CGContextRelease(context);
   223     CGColorSpaceRelease(colorSpace);
   209     CGColorSpaceRelease(colorSpace);
   224 
   210 
   225     UIImage *newImage;
   211     UIImage *newImage = [UIImage imageWithCGImage:imageMasked scale:screenScale orientation:UIImageOrientationUp];
   226     if ([self respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
       
   227         newImage = [UIImage imageWithCGImage:imageMasked scale:screenScale orientation:UIImageOrientationUp];
       
   228     else
       
   229         newImage = [UIImage imageWithCGImage:imageMasked];
       
   230 
   212 
   231     CGImageRelease(imageMasked);
   213     CGImageRelease(imageMasked);
   232 
   214 
   233     return newImage;
   215     return newImage;
   234 }
   216 }
   266     UIImage *bkgImg = [UIImage imageWithCGImage:image];
   248     UIImage *bkgImg = [UIImage imageWithCGImage:image];
   267     CGImageRelease(image);
   249     CGImageRelease(image);
   268     return bkgImg;
   250     return bkgImg;
   269 }
   251 }
   270 
   252 
       
   253 // this routine checks for the PNG size without loading it in memory
       
   254 // https://github.com/steipete/PSFramework/blob/master/PSFramework%20Version%200.3/PhotoshopFramework/PSMetaDataFunctions.m
       
   255 +(CGSize) imageSizeFromMetadataOf:(NSString *)aFileName {
       
   256     // File Name to C String.
       
   257     const char *fileName = [aFileName UTF8String];
       
   258     // source file
       
   259     FILE *infile = fopen(fileName, "rb");
       
   260     if (infile == NULL) {
       
   261         DLog(@"Can't open the file: %@", aFileName);
       
   262         return CGSizeZero;
       
   263     }
       
   264 
       
   265     // Bytes Buffer.
       
   266     unsigned char buffer[30];
       
   267     // Grab Only First Bytes.
       
   268     fread(buffer, 1, 30, infile);
       
   269     // Close File.
       
   270     fclose(infile);
       
   271 
       
   272     // PNG Signature.
       
   273     unsigned char png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
       
   274 
       
   275     // Compare File signature.
       
   276     if ((int)(memcmp(&buffer[0], &png_signature[0], 8))) {
       
   277         DLog(@"The file (%@) is not a PNG file", aFileName);
       
   278         return CGSizeZero;
       
   279     }
       
   280 
       
   281     // Calc Sizes. Isolate only four bytes of each size (width, height).
       
   282     int width[4];
       
   283     int height[4];
       
   284     for (int d = 16; d < (16 + 4); d++) {
       
   285         width[d-16] = buffer[d];
       
   286         height[d-16] = buffer[d+4];
       
   287     }
       
   288 
       
   289     // Convert bytes to Long (Integer)
       
   290     long resultWidth = (width[0] << (int)24) | (width[1] << (int)16) | (width[2] << (int)8) | width[3];
       
   291     long resultHeight = (height[0] << (int)24) | (height[1] << (int)16) | (height[2] << (int)8) | height[3];
       
   292 
       
   293     // Return Size.
       
   294     return CGSizeMake(resultWidth,resultHeight);
       
   295 }
       
   296 
   271 @end
   297 @end