2016-07-27 3 views
0

최근에 프로그래밍 방식으로 인터페이스를 다루기 시작했으며 이미지를 배경으로 설정하고 흐리게 만듭니다.프로그래밍 방식으로 xib에 흐림 이미지 배경 추가

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

을하지만, 내가 말한대로 나는이 새로운 종료하고 있습니다 : 나는 다음과 같이 샘플 코드를 본 적이있다. 어떤 사람이 어떻게 작동하는지 설명해 주실 수 있습니까? 어디에서 사용해야합니까?

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]; 

을하지만이 성능 문제가 발생할 수

답변

2

간단한, 그냥이 같은 시각 효과를 추가 할 수 있습니다. 따라서 가장 좋은 해결책은 이미지를 흐리게 처리하여 다시 그려야하고 흐려진 이미지를 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) 

이 옵션 값을 처리합니다. (있는 UIImage 확장으로 첨가)

+0

이 인 목표 -C 오른쪽? 스위프트를 통해 어떻게 동일한 작업을 수행 할 수 있습니까? :) –

+0

스위프트에는 동일한 API가 있습니다. 그냥 번역하십시오. – zylenv

0

스위프트 버전 3.1 :

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) 
    } 

} 
관련 문제