2016-07-27 6 views
0

Я недавно начал работать с моим интерфейсом программно, и я хочу установить изображение в качестве фона моей раскадровки, а затем сделать его размытым. Я видел примеры кодов, как следующее:Добавить blur изображение фона в xib программно

UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Wormhole.jpg"]]; 
[self.view insertSubview:backgroundView atIndex:0]; 

, но, как я сказал, что я бросить новичок в этом. Может кто-нибудь объяснить, как это работает? и где его использовать?

ответ

2

Простейшие, просто добавить визуальный эффект, как это:

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) 

Будьте уход за дополнительной стоимости.

+0

это цель-с правильно? как я могу сделать то же самое через Swift? :) –

+0

Swift имеет тот же API. Просто переведите его. – zylenv

0

Swift 3.1 версии (добавлен в качестве дополнения к UIImage):

extension UIImage { 

    func blurred(withRadius radius: Int) -> UIImage { 
     let context = CIContext(options: nil) 
     let inputImage = CIImage(cgImage: self.cgImage!) 
     let filter = CIFilter(name: "CIGaussianBlur")! 
     filter.setValue(inputImage, forKey: kCIInputImageKey) 
     filter.setValue(radius, forKey: "inputRadius") 
     let result = filter.value(forKey: kCIOutputImageKey) as! CIImage 
     let cgImage = context.createCGImage(result, from: inputImage.extent)! 
     return UIImage(cgImage: cgImage) 
    } 

} 
Смежные вопросы