2013-10-03 23 views
2

我想有一個模糊的效果,而像繪製在這張照片的右線:模糊效果,同時與Quartz 2D繪圖

Blur effect

目前,我用下面的代碼繪製,但這隻能畫左邊的圖片:

CGContextSetLineWidth(currentContext, thickness);  
CGContextSetLineCap(currentContext, kCGLineCapRound); 
CGContextBeginPath(currentContext); 
CGContextMoveToPoint(currentContext, x, y); 
CGContextAddLineToPoint(currentContext, x, y); 
CGContextMoveToPoint(currentContext, x, y); 
CGContextStrokePath(currentContext); 

對我有什麼建議嗎?

問候, 亞歷山大

回答

2
CIContext *context = [CIContext contextWithOptions:nil]; 
CIImage *inputImage = [[CIImage alloc] initWithImage:@"your image"]; 
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"]; 
[filter setValue:inputImage forKey:kCIInputImageKey]; 
[filter setValue:[NSNumber numberWithFloat:9.0f] forKey:@"inputRadius"]; 
CIImage *result = [filter valueForKey:kCIOutputImageKey]; 
CGImageRef cgImage = [context createCGImage:result fromRect:[result extent]]; 
UIImage *blurrImage = [UIImage imageWithCGImage:cgImage]; 

使用此代碼這會給你羅嗦效果。

+0

感謝您的回答,但喜歡用模糊效果繪製,而不是對UIImage應用效果。 – Alx

+0

繪製完成後,在CIImage中進行渲染。 'imageWithCGImage:'如果你需要的話。 – atreat

+0

您可能想使用'fromRect:[inputImage extent]'來代替。這可以確保'blurrImage'與'inputImage'的大小相同。使用'[結果範圍]'可能會導致圖像尺寸縮小。 –