project_files/HedgewarsMobile/Classes/UIImageExtra.m
changeset 3948 24daa33a3114
parent 3910 dd47efbdec46
child 3978 9660600e43cb
equal deleted inserted replaced
3947:709fdb89f76c 3948:24daa33a3114
    43     return scaledImage;
    43     return scaledImage;
    44 }
    44 }
    45 
    45 
    46 -(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint {
    46 -(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint {
    47     // create a contex of size of the background image
    47     // create a contex of size of the background image
    48     return [self mergeWith:secondImage atPoint:secondImagePoint atSize:self.size];
    48     return [self mergeWith:secondImage atPoint:secondImagePoint ofSize:self.size];
    49 }
    49 }
    50 
    50 
    51 -(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint atSize:(CGSize) resultingSize {
    51 -(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint ofSize:(CGSize) resultingSize {
    52     if (secondImage == nil) {
    52     if (secondImage == nil) {
    53         DLog(@"Warning, secondImage == nil");
    53         DLog(@"Warning, secondImage == nil");
    54         return self;
    54         return self;
    55     }
    55     }
       
    56     int w = resultingSize.width;
       
    57     int h = resultingSize.height;
       
    58     
       
    59     if (w == 0 || h == 0) {
       
    60         DLog(@"Can have 0 dimesions");
       
    61         return self;
       
    62     }
    56     
    63     
    57     // Create a bitmap graphics context; this will also set it as the current context
    64     // Create a bitmap graphics context; this will also set it as the current context
    58     UIGraphicsBeginImageContext(resultingSize);
    65     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    59 
    66     CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
    60     // draw the background image in the current context
    67     
    61     [self drawAtPoint:CGPointMake(0,0)];
    68     // draw the two images in the current context
    62 
    69     CGContextDrawImage(context, CGRectMake(0, 0, self.size.width, self.size.height), [self CGImage]);
    63     // draw the image on top of the first image (because the context is the same)
    70     CGContextDrawImage(context, CGRectMake(secondImagePoint.x, secondImagePoint.y, secondImage.size.width, secondImage.size.height), [secondImage CGImage]);
    64     [secondImage drawAtPoint:secondImagePoint];
    71     
    65 
    72     // Create bitmap image info from pixel data in current context
    66     // create an image from the current contex (not thread safe)
    73     CGImageRef imageRef = CGBitmapContextCreateImage(context);
    67     UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    74     
    68 
    75     // Create a new UIImage object
    69     // free drawing contex
    76     UIImage *resultImage = [UIImage imageWithCGImage:imageRef];
    70     UIGraphicsEndImageContext();
    77 
    71 
    78     // Release colorspace, context and bitmap information
    72     // return the resulting autoreleased image
    79     CGColorSpaceRelease(colorSpace);
       
    80     CGContextRelease(context);
       
    81     CFRelease(imageRef);
       
    82    
    73     return resultImage;
    83     return resultImage;
    74 }
    84 }
    75 
    85 
    76 -(id) initWithContentsOfFile:(NSString *)path andCutAt:(CGRect) rect {
    86 -(id) initWithContentsOfFile:(NSString *)path andCutAt:(CGRect) rect {
    77     // load image from path
    87     // load image from path
   213     CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
   223     CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
   214     [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
   224     [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
   215     CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference);
   225     CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference);
   216     CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor);
   226     CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor);
   217     CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, self.size.width, self.size.height));
   227     CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, self.size.width, self.size.height));
       
   228     // create an image from the current contex (not thread safe)
   218     UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
   229     UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
   219     UIGraphicsEndImageContext();
   230     UIGraphicsEndImageContext();
   220     return result;
   231     return result;
   221 }
   232 }
   222 
   233 
   223 +(UIImage *)whiteImage:(CGSize) ofSize {
   234 +(UIImage *)whiteImage:(CGSize) ofSize {
   224     // white rounded rectangle as background image for previewButton
       
   225     UIGraphicsBeginImageContext(ofSize);
   235     UIGraphicsBeginImageContext(ofSize);
   226     CGContextRef context = UIGraphicsGetCurrentContext();
   236     CGContextRef context = UIGraphicsGetCurrentContext();
   227     UIGraphicsPushContext(context);
   237     UIGraphicsPushContext(context);
   228 
   238 
   229     CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
   239     CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);