project_files/HedgewarsMobile/Classes/UIImageExtra.m
changeset 4446 3093791dc620
parent 4356 d1d26f8963a3
child 4461 2f4f5d649bcd
equal deleted inserted replaced
4445:40f7924beb43 4446:3093791dc620
    21 
    21 
    22 #import "UIImageExtra.h"
    22 #import "UIImageExtra.h"
    23 
    23 
    24 
    24 
    25 @implementation UIImage (extra)
    25 @implementation UIImage (extra)
       
    26 
       
    27 CGFloat getScreenScale(void) {
       
    28     if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
       
    29         return [UIScreen mainScreen].scale;
       
    30     else
       
    31         return 1.0f;
       
    32 }
    26 
    33 
    27 -(UIImage *)scaleToSize:(CGSize) size {
    34 -(UIImage *)scaleToSize:(CGSize) size {
    28     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");
    29 
    36 
    30     // 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
   191 }
   198 }
   192 
   199 
   193 -(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh {
   200 -(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh {
   194     CGFloat cornerWidth = sizewh.width;
   201     CGFloat cornerWidth = sizewh.width;
   195     CGFloat cornerHeight = sizewh.height;
   202     CGFloat cornerHeight = sizewh.height;
   196     CGFloat w = self.size.width;
   203     CGFloat theScale = getScreenScale();
   197     CGFloat h = self.size.height;
   204     CGFloat w = self.size.width * theScale;
       
   205     CGFloat h = self.size.height * theScale;
   198 
   206 
   199     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   207     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   200     CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
   208     CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
   201 
   209 
   202     CGContextBeginPath(context);
   210     CGContextBeginPath(context);
   209 
   217 
   210     CGImageRef imageMasked = CGBitmapContextCreateImage(context);
   218     CGImageRef imageMasked = CGBitmapContextCreateImage(context);
   211     CGContextRelease(context);
   219     CGContextRelease(context);
   212     CGColorSpaceRelease(colorSpace);
   220     CGColorSpaceRelease(colorSpace);
   213 
   221 
   214     UIImage *newImage = [UIImage imageWithCGImage:imageMasked];
   222     UIImage *newImage = [UIImage imageWithCGImage:imageMasked scale:theScale orientation:UIImageOrientationUp];
   215     CGImageRelease(imageMasked);
   223     CGImageRelease(imageMasked);
   216 
   224 
   217     return newImage;
   225     return newImage;
   218 }
   226 }
   219 
   227