Iphone 如何为可拉伸图像着色

Iphone 如何为可拉伸图像着色,iphone,ios,objective-c,ipad,uikit,Iphone,Ios,Objective C,Ipad,Uikit,我想为可拉伸的UIImage上色,但我无法使其工作。 在这篇文章的帮助下:我已经成功地给我的UIImage上色了,但是当我使它可拉伸时,颜色并没有设置在“新”的框架上 以下是我如何使用类别设置颜色: + (UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color { UIImage *image = [UIImage imageNamed:name]; if (color == nil) re

我想为可拉伸的UIImage上色,但我无法使其工作。 在这篇文章的帮助下:我已经成功地给我的UIImage上色了,但是当我使它可拉伸时,颜色并没有设置在“新”的框架上

以下是我如何使用类别设置颜色:

+ (UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color
{
    UIImage *image = [UIImage imageNamed:name];
    if (color == nil)
        return image;
    CGRect rect = CGRectMake(0.f, 0.f, image.size.width, image.size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClipToMask(context, rect, image.CGImage);
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return [UIImage imageWithCGImage:img.CGImage
                               scale:1.0
                         orientation:UIImageOrientationDownMirrored];
}
感谢您的任何建议……

此代码应适用于:

+ (UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)theColor
{
    UIImage *baseImage = [UIImage imageNamed:name];
    UIGraphicsBeginImageContext(baseImage.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect area = CGRectMake(0, 0, baseImage.size.width, baseImage.size.height);
    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -area.size.height);
    CGContextSaveGState(ctx);
    CGContextClipToMask(ctx, area, baseImage.CGImage);
    [theColor set];
    CGContextFillRect(ctx, area);
    CGContextRestoreGState(ctx);
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    CGContextDrawImage(ctx, area, baseImage.CGImage);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

您的包中有一个图像,您可以使用上面的方法对其进行着色,这不会修改原始图像,但会返回一个新图像。这个新图像是一个普通图像,这意味着您看到的颜色是图像中的实际位。如果现在从新创建的图像(而不是原始图像)创建新的可拉伸图像,则该图像应为正确的颜色


如果没有,请创建一个小的演示项目,并将其发布到DropBox上,让我们来看看。

能否粘贴创建彩色图像的方式,然后使其可拉伸?