![](/img/trans.png)
[英]Quartz UIImage with rounded corners and transparent background on iOS
[英]how to find transparent area in uiimage in ios?
嗨,朋友們,我正在開發填色游戲。我知道如何在全圖上着色。但是我想在透明區域上圖。
這是我為圖像着色的代碼
-(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;
}
如何在圖像中找到透明區域並填充顏色..?
首先填充顏色以完成整色。 矩形的大小將為圖像寬度和圖像高度。 現在在同一矩形上繪制圖像。
CGContextSetFillColorWithColor( context, [UIColor redColor].CGColor );
CGContextFillRect( context, rect );
CGContextDrawImage(context, rect, [UIImage imageNamed:@"myImage"].CGImage);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.