gpt4 book ai didi

iPhone:模糊 UIImage

转载 作者:行者123 更新时间:2023-12-03 18:31:50 28 4
gpt4 key购买 nike

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

iPhone 显然知道如何模糊图像,如 it does that when it draws shadows .

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

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

最佳答案

试试这个(找到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;
}

并像这样使用它:

UIImage *blurredImage = [originalImage imageWithGaussianBlur];

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

关于iPhone:模糊 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1356250/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com