最简单的,只需要加一个视觉效果是这样的:
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Wormhole.jpg"]];
[self.view insertSubview:backgroundView atIndex:0];
UIVisualEffectView *effect = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
[backgroundView addSubview:effect];
但是,这可能会导致性能问题。所以最好的解决方案应该是用模糊重绘图像,并将模糊图像设置为backgroundView的图像。 如何造成图像模糊,看below:
UIImageView *backgroundView = [[UIImageView alloc] init];
[self.view insertSubview:backgroundView atIndex:0];
UIImage *image = [UIImage imageNamed:@"Wormhole.jpg"];
//create blurred image
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *inputImage = [CIImage imageWithCGImage:image.CGImage];
//setting up Gaussian Blur (we could use one of many filters offered by Core Image)
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
[filter setValue:inputImage forKey:kCIInputImageKey];
[filter setValue:[NSNumber numberWithFloat:15.0f] forKey:@"inputRadius"];
CIImage *result = [filter valueForKey:kCIOutputImageKey];
CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];
//add our blurred image
backgroundView.image = [UIImage imageWithCGImage:cgImage];
雨燕代码:
let backgroundView = UIImageView()
self.view.addSubview(backgroundView)
let image = UIImage(named: "Wormhole.jpg")
let context = CIContext(options: nil)
let inputImage = CIImage(CGImage: image!.CGImage!)
let filter = CIFilter(name: "CIGaussianBlur")
filter!.setValue(inputImage, forKey: kCIInputImageKey)
filter!.setValue(15, forKey: "inputRadius")
let result = filter!.valueForKey(kCIOutputImageKey) as? CIImage
let cgImage = context.createCGImage(result!, fromRect: inputImage.extent)
backgroundView.image = UIImage(CGImage: cgImage)
要小心选购价值。
这是目标c正确?我怎么能通过Swift做同样的事情? :) –
Swift具有相同的API。只需翻译它。 – zylenv