這樣的事情也可以工作。基本上:不是使用剪切路徑,而是簡單地使用blendmode。 ,在這個例子中,漸變被緩存在CGLayer中。
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGColorSpaceRef cspace = CGColorSpaceCreateDeviceRGB();
CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(ctx,self.bounds);
CGFloat w = self.bounds.size.width;
CGFloat h = self.bounds.size.height;
CGFloat dh = (w-h)/2;
CGLayerRef l = CGLayerCreateWithContext(ctx,CGSizeMake(h,48.0f),NULL);
CGContextRef lctx = CGLayerGetContext(l);
float comp[] = { .2,.5,1.0,1.0,1.0,1.0,1.0,1.0};
CGGradientRef gradient = CGGradientCreateWithColorComponents(cspace, comp, NULL, 2);
CGContextDrawLinearGradient(lctx, gradient,CGPointMake(0,0),CGPointMake(0,48), 0);
CGContextSaveGState(ctx);
CGContextSetBlendMode(ctx,kCGBlendModeDarken);
for(int n=1;n<5;n++)
{
CGContextTranslateCTM(ctx,w/2.0,h/2.0);
CGContextRotateCTM(ctx, M_PI_2);
CGContextTranslateCTM(ctx,-w/2.0,-h/2.0);
CGContextDrawLayerAtPoint(ctx,CGPointMake((n%2)*dh,(n%2)*-dh),l);
}
CGContextRestoreGState(ctx);
使用CGGradient每條邊會更快,但如果這仍然太慢,你可以使用一個UIImage作爲高速緩存(提請UIImage的一次,然後繪製圖像在需要時查看) – rpetrich 2009-04-22 14:58:51
呀,我認爲無論我採用什麼方法來繪製圖片,緩存圖片和結果都可能是一個好主意。但我有點像程序化繪圖的想法,而不是在應用程序中發佈PNG。 – 2009-04-23 02:12:27