2009-09-28 74 views

回答

0

你真正需要的是CoreImage API中的图像过滤器。不幸的是CoreImage在iPhone上不支持(除非最近改变了,我错过了它)。这里要小心,因为IIRC,它们在SIM中可用 - 但不在设备上。

AFAIK没有其他的方法可以正确处理原生库,尽管我之前通过在顶部创建一个额外的图层来制作一个模糊的图层,它是下面的内容的副本,由像素或两个并且具有低α值。为了获得适当的模糊效果,我唯一能够做到的方式是在Photoshop或类似的离线模式下进行。

想知道是否还有更好的方法,但据我所知,这是目前的情况。

+1

嗯,有好消息,如果你”重新iOS5 :) – steipete 2011-08-10 15:12:27

+0

不幸的是,iOS 5上的Core Image不包含任何模糊的过滤器。 :( – LucasTizma 2012-01-13 05:00:36

6

苹果有一个名为GLImageProcessing一个伟大的示例程序,其中包括一个非常快的模糊/使用OpenGL ES 1.1锐化效果(这意味着它适用于所有的iPhone,而不仅仅是3GS)。

如果你没有相当经验与OpenGL,代码可能会让你的头受伤。

6

走下OpenGL路线感觉像疯了一样矫枉过正,以满足我的需求(模糊了图像上的一个接触点)。相反,我实现了一个简单的模糊过程,它需要一个接触点,创建一个包含该接触点的矩形,在那个点上对图像进行采样,然后将示例图像在源矩形顶部重新倒置几次,略微偏移一些不同的不透明度。这产生了一个相当不错的穷人的模糊效果,没有疯狂的代码和复杂性。代码如下:


- (UIImage*)imageWithBlurAroundPoint:(CGPoint)point { 
    CGRect    bnds = CGRectZero; 
    UIImage*   copy = nil; 
    CGContextRef  ctxt = nil; 
    CGImageRef   imag = self.CGImage; 
    CGRect    rect = CGRectZero; 
    CGAffineTransform tran = CGAffineTransformIdentity; 
    int    indx = 0; 

    rect.size.width = CGImageGetWidth(imag); 
    rect.size.height = CGImageGetHeight(imag); 

    bnds = rect; 

    UIGraphicsBeginImageContext(bnds.size); 
    ctxt = UIGraphicsGetCurrentContext(); 

    // Cut out a sample out the image 
    CGRect fillRect = CGRectMake(point.x - 10, point.y - 10, 20, 20); 
    CGImageRef sampleImageRef = CGImageCreateWithImageInRect(self.CGImage, fillRect); 

    // Flip the image right side up & draw 
    CGContextSaveGState(ctxt); 

    CGContextScaleCTM(ctxt, 1.0, -1.0); 
    CGContextTranslateCTM(ctxt, 0.0, -rect.size.height); 
    CGContextConcatCTM(ctxt, tran); 

    CGContextDrawImage(UIGraphicsGetCurrentContext(), rect, imag); 

    // Restore the context so that the coordinate system is restored 
    CGContextRestoreGState(ctxt); 

    // Cut out a sample image and redraw it over the source rect 
    // several times, shifting the opacity and the positioning slightly 
    // to produce a blurred effect 
    for (indx = 0; indx < 5; indx++) { 
     CGRect myRect = CGRectOffset(fillRect, 0.5 * indx, 0.5 * indx); 
     CGContextSetAlpha(ctxt, 0.2 * indx); 
     CGContextScaleCTM(ctxt, 1.0, -1.0); 
     CGContextDrawImage(ctxt, myRect, sampleImageRef); 
    } 

    copy = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return copy; 
} 
+0

嘿布雷克你能指导我如何实现模糊效果,同时绘图??我发布了关于SO的问题:[在使用OpenGL-ES的油漆应用程序中的模糊效果(湿湿效应)](http:// stackoverflow。 com/questions/6980402/blur-effect-wet-in-wet-effect-in-paint-application-using-opengl-es) – 2011-08-11 11:03:45

+0

任何建议或hekp被赞赏.. – 2011-08-11 11:04:28