最簡單的,只需要加一個視覺效果是這樣的:
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