2014-11-26 2 views
0

카테고리를 사용하여 흐림 효과를 추가하려고합니다. 내가 클래스에서 직접이 기능을 추가하는 경우카테고리가있는 iOS 이미지 흐림 효과

UIImage *image = [UIImage imageNamed:@"xxx"] 
UIImageView *page = [[UIImageView alloc] initWithImage:[UIImage blurImageWithImage:image withView:self]]; 

, 그것은 작동하지만, 나는있는 UIImage 범주에 그것을 할 경우 :

+ (UIImage *)blurImageWithImage:(UIImage*) imageName withView:(UIView*)view { 
UIImage *sourceImage = imageName; 
CIImage *inputImage = [CIImage imageWithCGImage:sourceImage.CGImage]; 

// Apply Affine-Clamp filter to stretch the image so that it does not 
// look shrunken when gaussian blur is applied 
CGAffineTransform transform = CGAffineTransformIdentity; 
CIFilter *clampFilter = [CIFilter filterWithName:@"CIAffineClamp"]; 
[clampFilter setValue:inputImage forKey:@"inputImage"]; 
[clampFilter setValue:[NSValue valueWithBytes:&transform objCType:@encode(CGAffineTransform)] forKey:@"inputTransform"]; 

// Apply gaussian blur filter with radius of 30 
CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"]; 
[gaussianBlurFilter setValue:clampFilter.outputImage forKey: @"inputImage"]; 
[gaussianBlurFilter setValue:@10 forKey:@"inputRadius"]; 

CIContext *context = [CIContext contextWithOptions:nil]; 
CGImageRef cgImage = [context createCGImage:gaussianBlurFilter.outputImage fromRect:[inputImage extent]]; 

// Set up output context. 
UIGraphicsBeginImageContext(view.frame.size); 
CGContextRef outputContext = UIGraphicsGetCurrentContext(); 

// Invert image coordinates 
CGContextScaleCTM(outputContext, 1.0, -1.0); 
CGContextTranslateCTM(outputContext, 0, view.frame.size.height); 

// Draw base image. 
CGContextDrawImage(outputContext, view.frame, cgImage); 

// Apply white tint 
CGContextSaveGState(outputContext); 
CGContextSetFillColorWithColor(outputContext, [UIColor colorWithWhite:1 alpha:0.2].CGColor); 
CGContextFillRect(outputContext, view.frame); 
CGContextRestoreGState(outputContext); 

// Output image is ready. 
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

return outputImage; } 

는 나는이 같은 UIView의 내부에이 함수를 호출합니다.

답변

0

문맥 변환을 할 때 "-"추가하는 것을 잊었습니다. 그래서 내가 한 일은 클래스 메서드를 만드는 것입니다.

인터페이스 :

+ (UIImage *)blurImageWithImageName:(NSString*) imageName withView:(UIView*)view; 

구현 :

+ (UIImage *)blurImageWithImageName:(NSString*) imageName withView:(UIView*)view { 
    UIImage *sourceImage = [UIImage imageNamed:imageName]; 
    CIImage *inputImage = [CIImage imageWithCGImage:sourceImage.CGImage]; 

    // Apply Affine-Clamp filter to stretch the image so that it does not 
    // look shrunken when gaussian blur is applied 
    CGAffineTransform transform = CGAffineTransformIdentity; 
    CIFilter *clampFilter = [CIFilter filterWithName:@"CIAffineClamp"]; 
    [clampFilter setValue:inputImage forKey:@"inputImage"]; 
    [clampFilter setValue:[NSValue valueWithBytes:&transform objCType:@encode(CGAffineTransform)] forKey:@"inputTransform"]; 

    // Apply gaussian blur filter with radius of 30 
    CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"]; 
    [gaussianBlurFilter setValue:clampFilter.outputImage forKey: @"inputImage"]; 
    [gaussianBlurFilter setValue:@10 forKey:@"inputRadius"]; 

    CIContext *context = [CIContext contextWithOptions:nil]; 
    CGImageRef cgImage = [context createCGImage:gaussianBlurFilter.outputImage fromRect:[inputImage extent]]; 

    // Set up output context. 
    UIGraphicsBeginImageContext(view.frame.size); 
    CGContextRef outputContext = UIGraphicsGetCurrentContext(); 

    // Invert image coordinates 
    CGContextScaleCTM(outputContext, 1.0, -1.0); 
    CGContextTranslateCTM(outputContext, 0, -view.frame.size.height); 

    // Draw base image. 
    CGContextDrawImage(outputContext, view.frame, cgImage); 

    // Apply white tint 
    CGContextSaveGState(outputContext); 
    CGContextSetFillColorWithColor(outputContext, [UIColor colorWithWhite:1 alpha:0.2].CGColor); 
    CGContextFillRect(outputContext, view.frame); 
    CGContextRestoreGState(outputContext); 

    // Output image is ready. 
    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return outputImage; 


    } 
1

이전에 똑같은 문제에 직면했습니다. 그러나 완전히 해결책을 얻고 있습니다.

다음 단계를 따르십시오. 흐림 이미지 기능이 올바르게 작동하는지 확인하십시오. 1) 클래스 메소드 대신 카테고리 추가 인스턴스 메소드. 예. 전

- (UIImage *)blurImageWithImage:(UIImage*) imageName withView:(UIView*)view 

2) VC 3) 사용 범주에서 가져 오기 카테고리,

UIImage *image = [UIImage imageNamed:@"xxx"] 
UIImageView *page = [[UIImageView alloc] initWithImage:[image blurImageWithImage:image withView:self]]; 

날이 솔루션은 당신을 위해 잘 작동 알려주십시오.

+0

이 작동하지만 분명히 내가 또 다른 문제가 있었다. 어쨌든 고마워요! – ordinaryman09