1

我在 iOS 中屏蔽了 UIImage。我现在想去掉那个面具。我将如何实现它。这是我屏蔽图像的代码:

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;
4

1 回答 1

1

如何将原始图像存储为 ivar 以及何时只想删除蒙版:

@implementation ViewController { UIImage *coloredImg_orginal; }

在将蒙版添加到图像集 colourImg_orginal 之前。

colouredImg_orginal = self.calorie_image.image;

并在删除蒙版的功能中只设置原始图像而不是蒙版图像

self.calorie_image.image = colouredImg_orginal;

(对不起,我的英语不好)

于 2014-02-05T13:07:27.313 回答