关于iphone:在更改滑块时模糊UIImage

Blur an UIImage on change of slider

我尝试了高斯模糊并检查了关于stackoverflow的所有问题,但是没有一个问题解决了我的崩溃问题。请帮忙,除了高斯模糊算法以外,还有其他方法可以模糊图像。我的图像大小为768x1024,循环迭代2 * 1024 * 768次,这是不可行的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
CGContextRef NYXImageCreateARGBBitmapContext(const size_t width, const size_t height, const size_t bytesPerRow)
{
/// Use the generic RGB color space
/// We avoid the NULL check because CGColorSpaceRelease() NULL check the value anyway, and worst case scenario = fail to create context
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

/// Create the bitmap context, we want pre-multiplied ARGB, 8-bits per component
CGContextRef bmContext = CGBitmapContextCreate(NULL, width, height, 8/*Bits per component*/, bytesPerRow, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);

CGColorSpaceRelease(colorSpace);

return bmContext;
}



 -(UIImage*)blurredImageUsingGaussFactor:(NSUInteger)gaussFactor andPixelRadius:(NSUInteger)pixelRadius
{
CGImageRef cgImage = self.CGImage;
const size_t originalWidth = CGImageGetWidth(cgImage);
const size_t originalHeight = CGImageGetHeight(cgImage);
const size_t bytesPerRow = originalWidth * 4;
CGContextRef context = NYXImageCreateARGBBitmapContext(originalWidth, originalHeight, bytesPerRow);
if (!context)
    return nil;

unsigned char *srcData, *destData, *finalData;

size_t width = CGBitmapContextGetWidth(context);
size_t height = CGBitmapContextGetHeight(context);
size_t bpr = CGBitmapContextGetBytesPerRow(context);
size_t bpp = CGBitmapContextGetBitsPerPixel(context) / 8;
CGRect rect = {{0.0f, 0.0f}, {width, height}};

CGContextDrawImage(context, rect, cgImage);

// Now we can get a pointer to the image data associated with the bitmap
// context.
srcData = (unsigned char*)CGBitmapContextGetData(context);
if (srcData != NULL)
{
    size_t dataSize = bpr * height;
    finalData = malloc(dataSize);
    destData = malloc(dataSize);
    memcpy(finalData, srcData, dataSize);
    memcpy(destData, srcData, dataSize);

    int sums[gaussFactor];
    size_t i, /*x, y,*/ k;
    int gauss_sum = 0;
    size_t radius = pixelRadius * 2 + 1;
    int *gauss_fact = malloc(radius * sizeof(int));

    for (i = 0; i < pixelRadius; i++)
    {
        gauss_fact[i] = 1 + (gaussFactor * i);
        gauss_fact[radius - (i + 1)] = 1 + (gaussFactor * i);
        gauss_sum += (gauss_fact[i] + gauss_fact[radius - (i + 1)]);
    }
    gauss_fact[(radius - 1) / 2] = 1 + (gaussFactor*pixelRadius);
    gauss_sum += gauss_fact[(radius - 1) / 2];

    unsigned char *p1, *p2, *p3;

    for (size_t y = 0; y < height; y++)
    {
        for (size_t x = 0; x < width; x++)
        {
            p1 = srcData + bpp * (y * width + x);
            p2 = destData + bpp * (y * width + x);

            for (i = 0; i < gaussFactor; i++)
                sums[i] = 0;

            for (k = 0; k < radius ; k++)
            {
                if ((y - ((radius - 1) >> 1) + k) < height)
                    p1 = srcData + bpp * ((y - ((radius - 1) >> 1) + k) * width + x);
                else
                    p1 = srcData + bpp * (y * width + x);

                for (i = 0; i < bpp; i++)
                    sums[i] += p1[i] * gauss_fact[k];

            }
            for (i = 0; i < bpp; i++)
                p2[i] = sums[i] / gauss_sum;
        }
    }
    for (size_t y = 0; y < height; y++)
    {
        for (size_t x = 0; x < width; x++)
        {
            p2 = destData + bpp * (y * width + x);
            p3 = finalData + bpp * (y * width + x);

            for (i = 0; i < gaussFactor; i++)
                sums[i] = 0;

            for(k = 0; k < radius ; k++)
            {
                if ((x - ((radius - 1) >> 1) + k) < width)
                    p1 = srcData + bpp * ( y * width + (x - ((radius - 1) >> 1) + k));
                else
                    p1 = srcData + bpp * (y * width + x);

                for (i = 0; i < bpp; i++)
                    sums[i] += p2[i] * gauss_fact[k];

            }
            for (i = 0; i < bpp; i++)
            {
                p3[i] = sums[i] / gauss_sum;
            }
        }
    }
}

size_t bitmapByteCount = bpr * height;

///////Here was the problem.. you had given srcData instead of destData.. Rest all
//were perfect...
CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, destData, bitmapByteCount, NULL);

CGImageRef blurredImageRef = CGImageCreate(width, height, CGBitmapContextGetBitsPerComponent(context), CGBitmapContextGetBitsPerPixel(context), CGBitmapContextGetBytesPerRow(context), CGBitmapContextGetColorSpace(context), CGBitmapContextGetBitmapInfo(context), dataProvider, NULL, true, kCGRenderingIntentDefault);

CGDataProviderRelease(dataProvider);
CGContextRelease(context);
if (destData)
    free(destData);
if (finalData)
    free(finalData);

UIImage* retUIImage = [UIImage imageWithCGImage:blurredImageRef];

CGImageRelease(blurredImageRef);

return retUIImage;
}


我已经对UIImage做了一个小的StackBlur扩展。 StackBlur与GaussianBlur接近,但速度更快。

在以下位置检查它:https://github.com/tomsoft1/StackBluriOS

小笔记...自述文件上有一个错字," normalized "代表" normalize "


不确定如何模糊图像。
这可能对您想要blur an UIImageView or any view

有所帮助

1
2
3
4
   UIView *myView = self.theImageView;
    CALayer *layer = [myView layer];
    [layer setRasterizationScale:0.25];
    [layer setShouldRasterize:YES];

您可以通过将栅格化比例设置回1来撤消它。

1
[layer setRasterizationScale:1.0];

更新:

下面的Apple示例代码包含模糊/锐化效果。 (使用Open GL)
看看是否有帮助,http://developer.apple.com/library/ios/#samplecode/GLImageProcessing/Introduction/Intro.html


您可能想要的是Box模糊算法。它比高斯模糊快10倍,并能产生不错的效果。我的代码在Android上运行,但还没有移植到iOS。这是来源。

只需大约10分钟即可移植到iOS。这些函数将按原样工作,您只需要访问图像字节(就像在上面的源代码中所做的一样),并将它们提供给函数即可。