0

在我的项目中,我正在显示这样的图像,

在此处输入图像描述

但根据用户进度我想隐藏部分图像。

示例:如果用户进程是 75%,则我的图像将显示如下,

在此处输入图像描述

一个想法是我将使用图像的一些变化并根据百分比显示图像。但是,如果我可以用一张图片以编程方式实现这一点,那就更好了。请帮助解决这个问题。

4

2 回答 2

1

您需要的是图像遮罩。从苹果文档-

图像蒙版是一个位图,它指定要绘制的区域,但不指定颜色。实际上,图像蒙版充当模板来指定在页面上放置颜色的位置。Quartz 使用当前的填充颜色来绘制图像蒙版。

因此,随着下载百分比的增加,您会降低uiimage下面的 alpha 值。希望这是您所需要的。

于 2013-07-04T04:44:43.320 回答
0

伙计们,我已经使用以下代码完成了它:

 UIImage * flagImage = [UIImage imageNamed:flagImageStr];  //this is image of the flag
    UIImage * maskImg = [UIImage imageNamed:@"mask4.png"];  //Created heart shape mask image

    UIImage *image = [self maskImage:flagImage withMask:maskImg];
    CALayer *layer = partialFlagIV.layer;  //partialFlagIV is my imageview name
    layer.shadowOpacity = .9;
    layer.shadowColor = [[UIColor blackColor] CGColor];
    layer.shadowOffset = CGSizeMake(0,0);
    layer.shadowRadius = 2;

    partialFlagIV.image = image;

    int percentage = [percentageStr integerValue];  //percentageStr value will decide how much part of image will be in other color
    int percentageValue = 100-percentage;

    int maskImgYValue = (percentageValue*80)/100;
    int imgYvalue;
    if (percentageValue<40) {
        imgYvalue = 305-(maskImgYValue*4);
    }else{
        imgYvalue = 305-(maskImgYValue*3.8);
    }

//This code is needed when i use app in iPad bcoze of size of partialFlagIV is larger in iPad
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        int baseValue = 80+(percentage*0.2);
        percentage = baseValue-percentage;
        maskImgYValue = maskImgYValue+percentage;
    }


    UIImageView *aImageView = [[UIImageView alloc] initWithFrame:CGRectMake(partialFlagIV.frame.origin.x, partialFlagIV.frame.origin.y, partialFlagIV.frame.size.width,maskImgYValue)];  //created another imageview to place it on the partialFlagIV so it will display like color field in partialFlagIV 


    aImageView.backgroundColor = [UIColor clearColor];
    image = [self imageByCropping:image toRect:CGRectMake(0, 0, image.size.width, image.size.height-imgYvalue)];  //this method is used to crop my flage image

    aImageView.image = [self newImageFromMaskImage:image  inColor:[UIColor colorWithRed:(186/255.0) green:(57/255.0) blue:(38/255.0) alpha:1]];   //this method will create mask image with given color
    [self.view addSubview:aImageView];
    [self.view bringSubviewToFront:aImageView];

//this method will crop the flag imge to heart shape
-(UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
    CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);

    UIImage *cropped = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    return cropped;
}

//this method will create color image using mask image
-(UIImage *) newImageFromMaskImage:(UIImage *)mask inColor:(UIColor *) color {
    CGImageRef maskImage = mask.CGImage;
    CGFloat width = mask.size.width;
    CGFloat height = mask.size.height;
    CGRect bounds = CGRectMake(0,0,width,height);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef bitmapContext = CGBitmapContextCreate(NULL, width, height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
    CGContextClipToMask(bitmapContext, bounds, maskImage);
    CGContextSetFillColorWithColor(bitmapContext, color.CGColor);
    CGContextFillRect(bitmapContext, bounds);

    CGImageRef mainViewContentBitmapContext = CGBitmapContextCreateImage(bitmapContext);
    CGContextRelease(bitmapContext);

    UIImage *result = [UIImage imageWithCGImage:mainViewContentBitmapContext];
    return result;
}

添加注释以解释代码。希望它对未来的用户有所帮助。

于 2013-08-14T17:00:58.157 回答