简体   繁体   English

如何在iOS的uiimage中找到透明区域?

[英]how to find transparent area in uiimage in ios?

Hi Friends, I am developing color filling game.i know how to color in full image.but i want to color the image in transparent area. 嗨,朋友们,我正在开发填色游戏。我知道如何在全图上着色。但是我想在透明区域上图。

Here my code for coloring image 这是我为图像着色的代码

-(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;
}

How to find transparent area in the image and fill the colors..? 如何在图像中找到透明区域并填充颜色..?

First fill the color for complete rect. 首先填充颜色以完成整色。 The rect will be of size image width and image height. 矩形的大小将为图像宽度和图像高度。 Now draw the image on same rect. 现在在同一矩形上绘制图像。

    CGContextSetFillColorWithColor( context, [UIColor redColor].CGColor );
    CGContextFillRect( context, rect );

    CGContextDrawImage(context, rect, [UIImage imageNamed:@"myImage"].CGImage);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM