CIGaussianBlur image size

前端 未结 3 1340
误落风尘
误落风尘 2020-12-28 09:07

I want to blur my view, and I use this code:

//Get a UIImage from the UIView
NSLog(@"blur capture");
UIGraphicsBeginImageContext(BlurContrainerView.         


        
3条回答
  •  不知归路
    2020-12-28 09:31

    The issue isn't that it's not blurring all of the image, but rather that the blur is extending the boundary of the image, making the image larger, and it's not lining up properly as a result.

    To keep the image the same size, after the line:

    CIImage *resultImage    = [gaussianBlurFilter valueForKey: @"outputImage"];
    

    You can grab the CGRect for a rectangle the size of the original image in the center of this resultImage:

    // note, adjust rect because blur changed size of image
    
    CGRect rect             = [resultImage extent];
    rect.origin.x          += (rect.size.width  - viewImage.size.width ) / 2;
    rect.origin.y          += (rect.size.height - viewImage.size.height) / 2;
    rect.size               = viewImage.size;
    

    And then use CIContext to grab that portion of the image:

    CIContext *context      = [CIContext contextWithOptions:nil];
    CGImageRef cgimg        = [context createCGImage:resultImage fromRect:rect];
    UIImage   *blurredImage = [UIImage imageWithCGImage:cgimg];
    CGImageRelease(cgimg);
    

    Alternatively, for iOS 7, if you go to the iOS UIImageEffects sample code and download iOS_UIImageEffects.zip, you can then grab the UIImage+ImageEffects category. Anyway, that provides a few new methods:

    - (UIImage *)applyLightEffect;
    - (UIImage *)applyExtraLightEffect;
    - (UIImage *)applyDarkEffect;
    - (UIImage *)applyTintEffectWithColor:(UIColor *)tintColor;
    - (UIImage *)applyBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor *)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage *)maskImage;
    

    So, to blur and image and lightening it (giving that "frosted glass" effect) you can then do:

    UIImage *newImage = [image applyLightEffect];
    

    Interestingly, Apple's code does not employ CIFilter, but rather calls vImageBoxConvolve_ARGB8888 of the vImage high-performance image processing framework. This technique is illustrated in WWDC 2013 video Implementing Engaging UI on iOS.

提交回复
热议问题