问题
I have been searching for an answer to this question in a few hours now, and I just can't figure it out. I want to add a gaussian blur effect to the image when i press the button "Button". The user is the one that is adding the image.
I have created an action for the "Button" based on sources from SO and other places on the web. It will not work. What am I doing wrong? Any code would be greatly appreciated. Here is my "Button" action:
- (IBAction)test:(id)sender {
CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
[gaussianBlurFilter setValue:imageView.image forKey: @"inputImage"];
[gaussianBlurFilter setValue:[NSNumber numberWithFloat: 10] forKey: @"inputRadius"];
}
If you need anything else that I have done to answer the question, please let me know :D
回答1:
Three observations:
You need to set the
inputImage
to be the theCIImage
from yourUIImage
:[gaussianBlurFilter setValue:[CIImage imageWithCGImage:[imageView.image CGImage]] forKey:kCIInputImageKey];
I don't see you grabbing the
outputImage
, e.g.:CIImage *outputImage = [gaussianBlurFilter outputImage];
And you presumably want to convert that
CIImage
back to aUIImage
.
So, putting that all together:
CIFilter *gaussianBlurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
[gaussianBlurFilter setDefaults];
CIImage *inputImage = [CIImage imageWithCGImage:[imageView.image CGImage]];
[gaussianBlurFilter setValue:inputImage forKey:kCIInputImageKey];
[gaussianBlurFilter setValue:@10 forKey:kCIInputRadiusKey];
CIImage *outputImage = [gaussianBlurFilter outputImage];
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[inputImage extent]]; // note, use input image extent if you want it the same size, the output image extent is larger
UIImage *image = [UIImage imageWithCGImage:cgimg];
CGImageRelease(cgimg);
Alternatively, if you go to the WWDC 2013 sample code (paid developer subscription required) and download iOS_UIImageEffects
, you can then grab the UIImage+ImageEffects
category. That provides a few new methods:
- (UIImage *)applyLightEffect;
- (UIImage *)applyExtraLightEffect;
- (UIImage *)applyDarkEffect;
- (UIImage *)applyTintEffectWithColor:(UIColor *)tintColor;
- (UIImage *)applyBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor *)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage *)maskImage;
So, to blur and image and lightening it (giving that "frosted glass" effect) you can then do:
UIImage *newImage = [image applyLightEffect];
Interestingly, Apple's code does not employ CIFilter
, but rather calls vImageBoxConvolve_ARGB8888 of the vImage high-performance image processing framework.
This technique is illustrated in WWDC 2013 video Implementing Engaging UI on iOS.
I know the question was about iOS 7, but now in iOS 8 one can add a blur effect to any UIView
object with UIBlurEffect
:
UIVisualEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
effectView.frame = imageView.bounds;
[imageView addSubview:effectView];
回答2:
This worked for me, however it is slow. I will do the transformation on app loading and use the once transformed image in many places.
This blur effect is for iOS 8.4. Swift language.
override func viewDidLoad()
{
super.viewDidLoad()
var backgroundImage = self.blurImage(UIImage(named: "background.jpg")!)
self.view.backgroundColor = UIColor(patternImage:backgroundImage)
}
func blurImage(imageToBlur:UIImage) -> UIImage
{
var gaussianBlurFilter = CIFilter(name: "CIGaussianBlur")
gaussianBlurFilter.setValue(CIImage(CGImage: imageToBlur.CGImage), forKey:kCIInputImageKey)
var inputImage = CIImage(CGImage: imageToBlur.CGImage)
var outputImage = gaussianBlurFilter.outputImage
var context = CIContext(options: nil)
var cgimg = context.createCGImage(outputImage, fromRect: inputImage.extent())
return UIImage(CGImage: cgimg)!
}
回答3:
The input of a CIFilter
needs to be a CIImage
not a UIImage
.
回答4:
Swift 4 UIImage
extension without force unwrapping:
extension UIImage {
/// Applies a gaussian blur to the image.
///
/// - Parameter radius: Blur radius.
/// - Returns: A blurred image.
func blur(radius: CGFloat = 6.0) -> UIImage? {
let context = CIContext()
guard let inputImage = CIImage(image: self) else { return nil }
guard let clampFilter = CIFilter(name: "CIAffineClamp") else { return nil }
clampFilter.setDefaults()
clampFilter.setValue(inputImage, forKey: kCIInputImageKey)
guard let blurFilter = CIFilter(name: "CIGaussianBlur") else { return nil }
blurFilter.setDefaults()
blurFilter.setValue(clampFilter.outputImage, forKey: kCIInputImageKey)
blurFilter.setValue(radius, forKey: kCIInputRadiusKey)
guard let blurredImage = blurFilter.value(forKey: kCIOutputImageKey) as? CIImage,
let cgImage = context.createCGImage(blurredImage, from: inputImage.extent) else { return nil }
let resultImage = UIImage(cgImage: cgImage, scale: scale, orientation: imageOrientation)
return resultImage
}
}
I highly recommend to blur the image on the background thread:
let image = UIImage(named: "myImage")
DispatchQueue.global(qos: .userInitiated).async {
let blurredImage = image.blur()
DispatchQueue.main.async {
self.myImageView.image = blurredImage
}
}
回答5:
Swift 2.0
Make an extension of UIImageView. (File-New-File-Empty Swift File -Name it Extensions. )
import Foundation
import UIKit
extension UIImageView{
//Method 1
func makeBlurImage(targetImageView:UIImageView?)
{
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = targetImageView!.bounds
blurEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] // for supporting device rotation
targetImageView?.addSubview(blurEffectView)
}
//Method 2
func convertToBlurImage(imageToBlur:UIImage) -> UIImage
{
let gaussianBlurFilter = CIFilter(name: "CIGaussianBlur")
gaussianBlurFilter!.setValue(CIImage(CGImage: imageToBlur.CGImage!), forKey:kCIInputImageKey)
let initialImage = CIImage(CGImage: imageToBlur.CGImage!)
let finalImage = gaussianBlurFilter!.outputImage
let finalImagecontext = CIContext(options: nil)
let finalCGImage = finalImagecontext.createCGImage(finalImage!, fromRect: initialImage.extent)
return UIImage(CGImage: finalCGImage)
}
}
Usage:
Using Method 1:
override func viewDidLoad() {
super.viewDidLoad()
let sampleImageView = UIImageView(frame: CGRectMake(0, 200, 300, 325))
let sampleImage:UIImage = UIImage(named: "ic_120x120")!
sampleImageView.image = sampleImage
//Convert To Blur Image Here
sampleImageView.makeBlurImage(sampleImageView)
self.view.addSubview(sampleImageView)
}
Using Method 2:
override func viewDidLoad() {
super.viewDidLoad()
let sampleImageView = UIImageView(frame: CGRectMake(0, 200, 300, 325))
let sampleImage:UIImage = UIImage(named: "ic_120x120")!
//Convert to Blurred Image Here
let sampleImage2 = sampleImageView.convertToBlurImage(sampleImage)
sampleImageView.image = sampleImage2
self.view.addSubview(sampleImageView)
}
回答6:
Here's an extension for image blur on
Swift 3
extension UIImage {
public func blurImage(radius: CGFloat) -> UIImage {
let inputImage = CIImage(image: self)!
let parameters: [String:Any] = [
kCIInputRadiusKey: radius,
kCIInputImageKey: inputImage
]
let filter = CIFilter(name: "CIGaussianBlur",
withInputParameters: parameters)!
let cgimg = CIContext().createCGImage(filter.outputImage!, from: inputImage.extent)
return UIImage(cgImage: cgimg!)
}
}
来源:https://stackoverflow.com/questions/19432773/creating-a-blur-effect-in-ios7