簡體   English   中英

通過objective-c模糊圖像

[英]Blur an image via objective-c

我想為應用程序模糊UIImage,你們中有沒有人使用Objective-C方法來模糊圖像?

我試圖找到一種方法或技術但卻找不到任何東西。 請幫助 !

您可以使用核心圖像過濾器。 http://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html

請查看https://gist.github.com/betzerra/5988604上的這個片段

//  Needs CoreImage.framework

- (UIImage *)blurredImageWithImage:(UIImage *)sourceImage{

    //  Create our blurred image
    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *inputImage = [CIImage imageWithCGImage:sourceImage.CGImage];

    //  Setting up Gaussian Blur
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [filter setValue:inputImage forKey:kCIInputImageKey];
    [filter setValue:[NSNumber numberWithFloat:15.0f] forKey:@"inputRadius"];
    CIImage *result = [filter valueForKey:kCIOutputImageKey];

    /*  CIGaussianBlur has a tendency to shrink the image a little, this ensures it matches 
     *  up exactly to the bounds of our original image */
    CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];

    UIImage *retVal = [UIImage imageWithCGImage:cgImage];

    if (cgImage) {
        CGImageRelease(cgImage);
    }

    return retVal;
}

您可以將UIVisualEffectView與視覺效果結合使用。 初始化visualEffect和effectView的元素,而不是添加到您的視圖或imgview,無論您想要的地方:)。 您也可以選擇EffectStyles。 代碼段:

UIVisualEffect *blurEffect;
blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];

UIVisualEffectView *visualEffectView;
visualEffectView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];

visualEffectView.frame = YourImgView.bounds;
[YourImgView addSubview:visualEffectView];

方案:

UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    blurEffectView.frame = self.view.bounds;
    blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    //ADD BLUR EFFECT VIEW IN MAIN VIEW
    [self.view addSubview:blurEffectView];

我建議使用Storyboard通過UIVisualEffectView實現模糊或活力。 有關更多信息,請查看我在https://github.com/Vaberer/BlurTransition上的示例項目,以演示如何使用它以及如何在UIVisualEffect中使用autolayout

Swift 2.2:

 func blurredImage(with sourceImage: UIImage) -> UIImage {


        let filter = CIFilter(name: "CIGaussianBlur")
        filter!.setValue(CIImage(image: sourceImage), forKey: kCIInputImageKey)
        filter!.setValue(0.8, forKey: kCIInputIntensityKey)
        let ctx = CIContext(options:nil)
        let cgImage = ctx.createCGImage(filter!.outputImage!, fromRect:filter!.outputImage!.extent)


        return UIImage(CGImage:cgImage)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM