我想为应用模糊UIImage, 你们中有没有人使用Objective-C方法模糊图像?
我试图找到一种方法或技术但却找不到任何东西。求救!
答案 0 :(得分:26)
您可以使用核心图像过滤器。 http://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html
从https://gist.github.com/betzerra/5988604
查看此代码段// Needs CoreImage.framework
- (UIImage *)blurredImageWithImage:(UIImage *)sourceImage{
// Create our blurred image
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *inputImage = [CIImage imageWithCGImage:sourceImage.CGImage];
// Setting up Gaussian Blur
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
[filter setValue:inputImage forKey:kCIInputImageKey];
[filter setValue:[NSNumber numberWithFloat:15.0f] forKey:@"inputRadius"];
CIImage *result = [filter valueForKey:kCIOutputImageKey];
/* CIGaussianBlur has a tendency to shrink the image a little, this ensures it matches
* up exactly to the bounds of our original image */
CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];
UIImage *retVal = [UIImage imageWithCGImage:cgImage];
if (cgImage) {
CGImageRelease(cgImage);
}
return retVal;
}
答案 1 :(得分:2)
您可以将UIVisualEffectView与视觉效果结合使用。初始化visualEffect和effectView的元素,而不是添加到您的视图或imgview,无论您想要的地方:)。您也可以选择EffectStyles。 代码段:
UIVisualEffect *blurEffect;
blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *visualEffectView;
visualEffectView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
visualEffectView.frame = YourImgView.bounds;
[YourImgView addSubview:visualEffectView];
答案 2 :(得分:2)
解决方案:
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
blurEffectView.frame = self.view.bounds;
blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//ADD BLUR EFFECT VIEW IN MAIN VIEW
[self.view addSubview:blurEffectView];
答案 3 :(得分:0)
我建议使用Storyboard来实现UIVisualEffectView
的模糊或活力。有关更多信息,请查看我在https://github.com/Vaberer/BlurTransition的示例项目,以演示如何使用它以及如何在UIVisualEffect中使用autolayout
答案 4 :(得分:0)
Swift 2.2:
func blurredImage(with sourceImage: UIImage) -> UIImage {
let filter = CIFilter(name: "CIGaussianBlur")
filter!.setValue(CIImage(image: sourceImage), forKey: kCIInputImageKey)
filter!.setValue(0.8, forKey: kCIInputIntensityKey)
let ctx = CIContext(options:nil)
let cgImage = ctx.createCGImage(filter!.outputImage!, fromRect:filter!.outputImage!.extent)
return UIImage(CGImage:cgImage)
}