project_files/HedgewarsMobile/Classes/UIImageExtra.m
changeset 4476 4bf74e158f44
parent 4461 2f4f5d649bcd
child 4478 05029b4d8490
equal deleted inserted replaced
4474:499748f6e80f 4476:4bf74e158f44
    33 
    33 
    34 -(UIImage *)scaleToSize:(CGSize) size {
    34 -(UIImage *)scaleToSize:(CGSize) size {
    35     DLog(@"warning - this is a very expensive operation, you should avoid using it");
    35     DLog(@"warning - this is a very expensive operation, you should avoid using it");
    36 
    36 
    37     // Create a bitmap graphics context; this will also set it as the current context
    37     // Create a bitmap graphics context; this will also set it as the current context
    38     UIGraphicsBeginImageContext(size);
    38     if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
       
    39         UIGraphicsBeginImageContextWithOptions(size, NO, getScreenScale());
       
    40     else
       
    41         UIGraphicsBeginImageContext(size);
    39 
    42 
    40     // Draw the scaled image in the current context
    43     // Draw the scaled image in the current context
    41     [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    44     [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    42 
    45 
    43     // Create a new image from current context
    46     // Create a new image from current context
    58 -(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint ofSize:(CGSize) resultingSize {
    61 -(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint ofSize:(CGSize) resultingSize {
    59     if (secondImage == nil) {
    62     if (secondImage == nil) {
    60         DLog(@"Warning, secondImage == nil");
    63         DLog(@"Warning, secondImage == nil");
    61         return self;
    64         return self;
    62     }
    65     }
    63     int w = resultingSize.width;
    66     CGFloat screenScale = getScreenScale();
    64     int h = resultingSize.height;
    67     int w = resultingSize.width * screenScale;
       
    68     int h = resultingSize.height * screenScale;
    65     
    69     
    66     if (w == 0 || h == 0) {
    70     if (w == 0 || h == 0) {
    67         DLog(@"Can have 0 dimesions");
    71         DLog(@"Can have 0 dimesions");
    68         return self;
    72         return self;
    69     }
    73     }
    71     // Create a bitmap graphics context; this will also set it as the current context
    75     // Create a bitmap graphics context; this will also set it as the current context
    72     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    76     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    73     CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
    77     CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
    74     
    78     
    75     // draw the two images in the current context
    79     // draw the two images in the current context
    76     CGContextDrawImage(context, CGRectMake(0, 0, self.size.width, self.size.height), [self CGImage]);
    80     CGContextDrawImage(context, CGRectMake(0, 0, self.size.width*screenScale, self.size.height*screenScale), [self CGImage]);
    77     CGContextDrawImage(context, CGRectMake(secondImagePoint.x, secondImagePoint.y, secondImage.size.width, secondImage.size.height), [secondImage CGImage]);
    81     CGContextDrawImage(context, CGRectMake(secondImagePoint.x*screenScale, secondImagePoint.y*screenScale, secondImage.size.width*screenScale, secondImage.size.height*screenScale), [secondImage CGImage]);
    78     
    82     
    79     // Create bitmap image info from pixel data in current context
    83     // Create bitmap image info from pixel data in current context
    80     CGImageRef imageRef = CGBitmapContextCreateImage(context);
    84     CGImageRef imageRef = CGBitmapContextCreateImage(context);
    81     
    85     
    82     // Create a new UIImage object
    86     // Create a new UIImage object
    83     UIImage *resultImage = [UIImage imageWithCGImage:imageRef];
    87     UIImage *resultImage;
       
    88     if ([self respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
       
    89         resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp];
       
    90     else
       
    91         resultImage = [UIImage imageWithCGImage:imageRef];
    84 
    92 
    85     // Release colorspace, context and bitmap information
    93     // Release colorspace, context and bitmap information
    86     CGColorSpaceRelease(colorSpace);
    94     CGColorSpaceRelease(colorSpace);
    87     CGContextRelease(context);
    95     CGContextRelease(context);
    88     CFRelease(imageRef);
    96     CFRelease(imageRef);
   198 }
   206 }
   199 
   207 
   200 -(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh {
   208 -(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh {
   201     CGFloat cornerWidth = sizewh.width;
   209     CGFloat cornerWidth = sizewh.width;
   202     CGFloat cornerHeight = sizewh.height;
   210     CGFloat cornerHeight = sizewh.height;
   203     CGFloat theScale = getScreenScale();
   211     CGFloat screenScale = getScreenScale();
   204     CGFloat w = self.size.width * theScale;
   212     CGFloat w = self.size.width * screenScale;
   205     CGFloat h = self.size.height * theScale;
   213     CGFloat h = self.size.height * screenScale;
   206 
   214 
   207     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   215     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   208     CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
   216     CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
   209 
   217 
   210     CGContextBeginPath(context);
   218     CGContextBeginPath(context);
   219     CGContextRelease(context);
   227     CGContextRelease(context);
   220     CGColorSpaceRelease(colorSpace);
   228     CGColorSpaceRelease(colorSpace);
   221 
   229 
   222     UIImage *newImage;
   230     UIImage *newImage;
   223     if ([self respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
   231     if ([self respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
   224         newImage = [UIImage imageWithCGImage:imageMasked scale:theScale orientation:UIImageOrientationUp];
   232         newImage = [UIImage imageWithCGImage:imageMasked scale:screenScale orientation:UIImageOrientationUp];
   225     else
   233     else
   226         newImage = [UIImage imageWithCGImage:imageMasked];
   234         newImage = [UIImage imageWithCGImage:imageMasked];
   227 
   235 
   228     CGImageRelease(imageMasked);
   236     CGImageRelease(imageMasked);
   229 
   237