iPhone:模糊UIImage

Ale*_*ysh 9 iphone image-manipulation quartz-2d

在我的iPhone应用程序中,我有一个黑白UIImage.我需要模糊该图像(高斯模糊会这样做).

iPhone清楚地知道如何模糊图像,因为它在绘制阴影时会这样做.

但是我没有在API中找到任何相关内容.

在没有硬件加速的情况下,我是否必须手工模糊?

dom*_*dom 15

试试这个(在这里找到):

@interface UIImage (ImageBlur)
- (UIImage *)imageWithGaussianBlur;
@end

@implementation UIImage (ImageBlur)
- (UIImage *)imageWithGaussianBlur {
    float weight[5] = {0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162};
    // Blur horizontally
    UIGraphicsBeginImageContext(self.size);
    [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[0]];
    for (int x = 1; x < 5; ++x) {
        [self drawInRect:CGRectMake(x, 0, self.size.width, self.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[x]];
        [self drawInRect:CGRectMake(-x, 0, self.size.width, self.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[x]];
    }
    UIImage *horizBlurredImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    // Blur vertically
    UIGraphicsBeginImageContext(self.size);
    [horizBlurredImage drawInRect:CGRectMake(0, 0, self.size.width, self.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[0]];
    for (int y = 1; y < 5; ++y) {
        [horizBlurredImage drawInRect:CGRectMake(0, y, self.size.width, self.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[y]];
        [horizBlurredImage drawInRect:CGRectMake(0, -y, self.size.width, self.size.height) blendMode:kCGBlendModePlusLighter alpha:weight[y]];
    }
    UIImage *blurredImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    //
    return blurredImage;
}
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

UIImage *blurredImage = [originalImage imageWithGaussianBlur];
Run Code Online (Sandbox Code Playgroud)

为了获得更强的模糊,只需将此效果应用两次或更多:)


tom*_*oft 6

在过去遇到同样的问题后,请检查此lib:

https://github.com/tomsoft1/StackBluriOS

也很容易使用:

UIImage *newIma=[oldIma stackBlur:radius];
Run Code Online (Sandbox Code Playgroud)


Gen*_*ode 0

基本上没有直接的 API 来实现模糊效果。您需要处理像素才能实现这一点。

iPhone 通过渐变而不是模糊来绘制阴影。