【问题标题】:how to find transparent area in uiimage in ios?如何在ios的uiimage中找到透明区域?
【发布时间】:2012-12-20 21:26:23
【问题描述】:

嗨朋友们, 我正在开发填色游戏。我知道如何为完整图像着色。但我想在透明区域为图像着色。

这是我为图像着色的代码

-(UIImage*)setCollor:(UIColor*)color image:(UIImage*)img
{
    UIGraphicsBeginImageContext(img.size);    
    CGContextRef context = UIGraphicsGetCurrentContext();

    // set the fill color
    [color setFill];

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

    // 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();

    //return the color-burned image
    return coloredImg;
}

如何在图像中找到透明区域并填充颜色..?

【问题讨论】:

    标签: iphone ios ios5 ios4 iphone-sdk-3.0


    【解决方案1】:

    首先填充完整矩形的颜色。矩形的大小为图像宽度和图像高度。现在在同一个矩形上绘制图像。

        CGContextSetFillColorWithColor( context, [UIColor redColor].CGColor );
        CGContextFillRect( context, rect );
    
        CGContextDrawImage(context, rect, [UIImage imageNamed:@"myImage"].CGImage);
    

    【讨论】:

    • 我正在做绘画应用程序。所以我想应用洪水填充算法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 2011-05-21
    • 1970-01-01
    • 2012-06-20
    • 2014-03-08
    相关资源
    最近更新 更多