Question

I am masking an UIImage in iOS. I want to remove that mask now. How will I achieve it. This is my code for masking the image :

UIColor *header1Color = [UIColor colorWithRed:0.286 green:0.286 blue:0.286 alpha:0.1];

        UIImage *img = self.calorie_image.image;
//        int width = img.size.width;  //308
//        int height = img.size.height; //67

        UIGraphicsBeginImageContext(img.size);

        CGContextRef context = UIGraphicsGetCurrentContext();

        [header1Color setFill];

        // translate/flip the graphics context (for transforming from CG* coords to UI* coords
        CGContextTranslateCTM(context, 0, img.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);

        // set the blend mode to color burn, and the original image
        CGContextSetBlendMode(context, kCGBlendModeColorBurn);
        CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
        CGContextDrawImage(context, rect, img.CGImage);

        //calorie_value = 230;
        int x = 200;
        int y = 0;
        int mwidth = 120;
        int mheight = 67;
        NSString *zone = @"";
        NSArray *min = [[NSArray alloc] initWithObjects:@"0", @"1200", @"1800", @"2200", nil];
        NSArray *max = [[NSArray alloc] initWithObjects:@"1200", @"1800", @"2200", @"3500", nil];

        NSArray *x_values = [[NSArray alloc] initWithObjects:@"42", @"137", @"200", @"320", nil];
        NSArray *mwidth_values = [[NSArray alloc] initWithObjects:@"278", @"183", @"120", @"0", nil];

        NSArray *zones = [[NSArray alloc] initWithObjects:@"red", @"green", @"orange", @"red", nil];

        for (int i=0; i < 4; i++) {
            if(calorie_value >= [min[i] integerValue] && calorie_value <= [max[i] integerValue]) {
                zone = zones[i];
                x = [x_values[i] integerValue];
                mwidth = [mwidth_values[i] integerValue];
                break;
            }
        }

        if([[DiabetaDbTransaction getToadyCalories] integerValue] > 0) {
            CGContextClearRect(context, CGRectMake(x,y,mwidth,mheight));
        }

        // set a mask that matches the shape of the image, then draw (color burn) a colored rectangle
        CGContextClipToMask(context, rect, img.CGImage);
        CGContextAddRect(context, rect);
        CGContextDrawPath(context,kCGPathFill);

        // generate a new UIImage from the graphics context we drew onto
        UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        self.calorie_image.image = coloredImg;
Was it helpful?

Solution

How about to store the orginal image as an ivar and when you want to remove mask just :

@implementation ViewController { UIImage *coloredImg_orginal; }

Before you add mask to image set coloredImg_orginal.

coloredImg_orginal = self.calorie_image.image;

and in the function to remove mask just set orgial image instead of the mask image

self.calorie_image.image = coloredImg_orginal;

(sorry for my bad english)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top