2017-07-19 66 views
0

我想画出这样的形状enter image description here如何绘制模糊形状?

我知道绘制圆的代码。但我不知道如何使褪色大小:

- (void)drawRect:(CGRect)rect { 
    [super drawRect:rect]; 

    CGContextRef c = UIGraphicsGetCurrentContext(); 
    CGContextSetShouldAntialias(c, YES); 

    // Fill background 
    CGContextSetFillColorWithColor(c, [UIColor colorWithWhite:1 alpha:1].CGColor); 
    CGContextAddRect(c, CGRectMake(0, 0, rect.size.width, rect.size.height)); 
    CGContextFillPath(c); 

    // Dark inside 
    if (_type == LDConstrainTypeCircle) { 
     CGContextAddEllipseInRect(c, self.blurRect); 
     CGContextClip(c); 
    } 
    CGContextSetFillColorWithColor(c, [UIColor blackColor].CGColor); 
    CGContextAddRect(c, self.blurRect); 
    CGContextFillPath(c); 
} 

如何使图像这样。我的项目是针对iOS 7.0和更高版本的。谢谢!

+0

请尝试在这里https://stackoverflow.com/questions/30953201/adding-blur-effect-to-background-in-swift/39887516#39887516 –

+0

使用CIFilter。 – matt

回答

0

您可以采取的特定元素的屏幕截图,并对其进行模糊处理

func captureScreenshot() -> UIImage { 

    UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale) 
    self.layer.render(in: UIGraphicsGetCurrentContext()!) 
    let image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    return image! 

} 

func addBlurEffect() { 
    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light) 
    let blurEffectView = UIVisualEffectView(effect: blurEffect) 
    blurEffectView.frame = self.bounds 

    blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] // for supporting device rotation 
    self.addSubview(blurEffectView) 

    UIView.animate(withDuration: 0.5) { 
     blurEffectView.effect = UIBlurEffect(style: UIBlurEffectStyle.light) 
    } 
} 
+0

看起来你的代码是捕获一个屏幕截图...我知道模糊的代码。但我不知道创建遮罩图像的代码。 – TienLe

+0

@TienLe添加了模糊代码 – ppinho

+0

在这种情况下是扩展UIImageView – ppinho

0

对于iOS 8.0+您UIImageView

var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light))  
visualEffectView.frame = imageView.bounds 
imageView.addSubview(visualEffectView) 
+0

感谢您的帮助。我的项目是针对iOS 7.0和更高版本的。我将编辑该问题:) – TienLe

0

你可以参考this blog使用UIVisualEffectView

本博客为您提供了三种模糊视图选项。

  1. 与核心映像框架
  2. 使用布拉德·拉尔森的GPUImage框架
  3. 使用苹果的UIImage + ImageEffects类别

  1. 与核心映像框架

    模糊模糊模糊模糊
    -(UIImage *)blurWithCoreImage:(UIImage *)sourceImage 
    { 
    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:@30 forKey:@"inputRadius"]; 
    
    CIContext *context = [CIContext contextWithOptions:nil]; 
    CGImageRef cgImage = [context createCGImage:gaussianBlurFilter.outputImage fromRect:[inputImage extent]]; 
    
    // Set up output context. 
    UIGraphicsBeginImageContext(self.view.frame.size); 
    CGContextRef outputContext = UIGraphicsGetCurrentContext(); 
    
    // Invert image coordinates 
    CGContextScaleCTM(outputContext, 1.0, -1.0); 
    CGContextTranslateCTM(outputContext, 0, -self.view.frame.size.height); 
    
    // Draw base image. 
    CGContextDrawImage(outputContext, self.view.frame, cgImage); 
    
    // Apply white tint 
    CGContextSaveGState(outputContext); 
    CGContextSetFillColorWithColor(outputContext, [UIColor colorWithWhite:1 alpha:0.2].CGColor); 
    CGContextFillRect(outputContext, self.view.frame); 
    CGContextRestoreGState(outputContext); 
    
    // Output image is ready. 
    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    
    return outputImage; 
    } 
    

  • 使用布拉德Larson的GPUImage框架
  • GPUImage是用于图像和视频处理,创建和布拉德维持一个开放源码的iOS框架模糊拉尔森。它包含一系列GPU加速过滤器,可应用于图像,实时相机视频和电影。

    GPUImage framework包含在本教程的源文件,但加入了框架,你自己的项目是很容易的:

    1. 开始通过下载框架或 GitHub的克隆库。
    2. 打开终端窗口,导航到GPUImage文件夹,然后运行 构建脚本build.sh来编译框架。
    3. 将GPUImage.framework从build文件夹复制到项目的 文件夹中,然后将其拖放到Project Navigator中。
    4. 然后,您可以通过导入 框架头使用GPUImage框架项目,#import <GPUImage/GPUImage.h>.

    的GPUImage框架包括类似于那些在核心映像框架过滤器。对于我们的示例应用程序,我们对两个过滤器感兴趣,GPUImageGaussianBlurFilterGPUImageiOSBlurFilter

    -(UIImage *)blurWithGPUImage:(UIImage *)sourceImage 
    { 
        // Gaussian Blur 
        GPUImageGaussianBlurFilter *blurFilter = [[GPUImageGaussianBlurFilter alloc] init]; 
        blurFilter.blurRadiusInPixels = 30.0; 
    
        return [blurFilter imageByFilteringImage: sourceImage]; 
    } 
    

    代替GPUImageGaussianblur类的,也可以使用类GPUImageiOSblur像这样:

    // iOS的模糊

    GPUImageiOSBlurFilter *blurFilter = [[GPUImageiOSBlurFilter alloc] init]; 
    blurFilter.blurRadiusInPixels = 30.0; 
    

  • 模糊使用Apple的UIImage + ImageEffects类别
  • 您可以从Apple的开发人员网站下载Apple的ImageEffects示例项目,并在您的项目中使用它。

    #import "UIImage+ImageEffects.h" 
    
    ... 
    
    - (UIImage *)blurWithImageEffects:(UIImage *)image 
    { 
        return [image applyBlurWithRadius:30 tintColor:[UIColor colorWithWhite:1 alpha:0.2] saturationDeltaFactor:1.5 maskImage:nil]; 
    } 
    

    希望这会对你有帮助。 快乐编码。 :-)