我使用這個代碼上色一個UIButton子類的一些圖片:如何平滑繪製圖像的邊緣?
UIImage *img = [self imageForState:controlState];
// begin a new image context, to draw our colored image onto
UIGraphicsBeginImageContextWithOptions(img.size, NO, 0.0f);
// get a reference to that context we created
CGContextRef context = UIGraphicsGetCurrentContext();
// set the fill color
[self.buttonColor setFill];
CGContextSetAllowsAntialiasing(context, true);
CGContextSetShouldAntialias(context, true);
// translate/flip the graphics context (for transforming from CG* coords to UI* coords
CGContextTranslateCTM(context, 0, img.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// set the blend mode to multiply, and the original image
CGContextSetBlendMode(context, kCGBlendModeScreen);
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 the colored image
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 colored image
[self setImage:coloredImg forState:controlState];
但圖像打印出來與粗糙的邊緣。我嘗試過使用屏幕,變亮和plusLighter混合模式,因爲一些圖像有白色部分,我想保持白色。我想要着色的唯一部分是黑色區域。我附加了原始按鈕圖像,並在它們被着色之後。我無法讓邊緣看起來不錯。當我將它們作爲使用多種混合模式着色的白色圖像時,它看起來好多了。但是我想使用黑色,所以我可以使用一種方法對其中有白色的圖像進行着色。我嘗試了抗鋸齒,這也沒有幫助。看起來它並不是反鋸齒。我還沒有和Core Graphics一起工作過,知道它有什麼問題。
編輯 這裏是原來的PNG圖像看起來像:
,這裏是它應該是什麼樣子:
這裏就是它看起來像:
大小如果不同,但您可以看到邊緣周圍的質量差。
你可以發佈一個你想要它看起來像什麼和它看起來像什麼樣子的樣機嗎? – 2012-08-08 20:54:16
我認爲這個問題與圖像外部的灰度像素來自抗鋸齒有關。不知道如何解決,雖然 – Marty 2012-08-08 21:43:28