iPhone:模糊 UIImage

发布于 2024-08-02 22:50:23 字数 336 浏览 2 评论 0原文

在我的 iPhone 应用程序中,我有一个黑白 UIImage。我需要模糊该图像(高斯模糊即可)。

iPhone 显然知道如何模糊图像,正如 它所做的那样当它绘制阴影时

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

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

In my iPhone application I have a black-and-white UIImage. I need to blur that image (Gaussian blur would do).

iPhone clearly knows how to blur images, as it does that when it draws shadows.

However I did not found anything related in the API.

Do I have to do blurring by hand, without hardware acceleration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

何以笙箫默 2024-08-09 22:50:23

试试这个(在此处找到):

@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;
}

这样使用它:

UIImage *blurredImage = [originalImage imageWithGaussianBlur];

并像 只需应用此效果两次或更多次即可获得更强的模糊效果:)

Try this (found here):

@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;
}

And use it like this:

UIImage *blurredImage = [originalImage imageWithGaussianBlur];

To get stronger blur just apply this effect twice or more :)

超可爱的懒熊 2024-08-09 22:50:23

过去遇到同样的问题后,检查这个库:

https://github.com/tomsoft1/StackBluriOS

也非常容易使用:

UIImage *newIma=[oldIma stackBlur:radius];

After having the same problem by the past, check this lib:

https://github.com/tomsoft1/StackBluriOS

Very easy to use also:

UIImage *newIma=[oldIma stackBlur:radius];
生活了然无味 2024-08-09 22:50:23

考虑通过 CIFilter 来完成这项工作:

为 iOS 开发核心图像过滤器

Consider approaching the job via CIFilter:

Developing Core Image Filters for iOS

゛时过境迁 2024-08-09 22:50:23

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

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

Basically there is no a straight forward API to implement the blur effect. You need to process the pixels to accomplish that.

iPhone draw shadows by means of gradient and not through blurring.

浅浅 2024-08-09 22:50:23

要模糊图像,您可以使用卷积矩阵。这是应用卷积矩阵的示例代码,这是卷积矩阵概述以及一些示例矩阵(包括模糊和高斯模糊)。

To blur an image, you would use a convolution matrix. Here is the sample code to apply a convolution matrix, and here is an overview of convolution matrices as well as some sample matrices (including blur and gaussian blur).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文