2014-11-26 8 views
0

Я пытаюсь добавить эффект размытия с использованием категории.iOS эффект размытия изображения с категорией

+ (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 так:

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

Если добавить эту функцию непосредственно в классе, он работает, но если я делаю это в категории UIImage.

ответ

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) В категории добавить метод экземпляра вместо метода класса. ex.

- (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