【问题标题】:iOS image blur effect with categoryiOS 带有类别的图像模糊效果
【发布时间】:2014-11-26 18:58:13
【问题描述】:

我正在尝试使用类别添加模糊效果。

+ (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 类别中这样做,则不行。

【问题讨论】:

    标签: ios objective-c-category uiblureffect


    【解决方案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]];
    

    让我知道这个解决方案对你很有效。

    【讨论】:

    • 这行得通,但显然我还有另一个问题。还是谢谢!
    【解决方案2】:

    原来问题是我在进行上下文翻译时忘记添加“-”。 所以我最终做的是创建一个类方法。

    界面:

    + (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;
    
    
       }
    

    【讨论】:

      猜你喜欢
      • 2015-04-26
      • 1970-01-01
      • 2012-03-17
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      • 2013-09-20
      • 2014-01-30
      • 1970-01-01
      相关资源
      最近更新 更多