iOS中常用的毛玻璃處理

  //1.模糊毛玻璃效果實現方案一
    //利用系統的CoreImage(濾鏡)  濾鏡處理的過程比較慢,會造成加載圖片緩慢的現象(等一會纔看到圖片),儘量放到子線程執行
    UIImage *image = [UIImage imageNamed:@"1_1280x800"];
    //1.創建CIImage
    CIImage *ciImage = [[CIImage alloc] initWithImage:image];
    //2.創建濾鏡CIFilter
    CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
    //3.將CIImage輸入到濾鏡中
    [blurFilter setValue:ciImage forKey:kCIInputImageKey];
    //4.設置模糊度
    [blurFilter setValue:@(10) forKey:@"inputRadius"];
    //5.將處理好的圖片輸出
    CIImage *outCiImage = [blurFilter valueForKey:kCIOutputImageKey];
    //6.CIContext (option 參數爲nil代表用CPU渲染,若想用GPU渲染請查看此參數)
    CIContext *context = [CIContext contextWithOptions:nil];
    //7.獲取CGImage句柄
    CGImageRef outCGImage = [context createCGImage:outCiImage fromRect:[outCiImage extent]];
    //8.獲取最終的圖片
    UIImage *blurImage = [UIImage imageWithCGImage:outCGImage];
    //9.釋放CGImage
    CGImageRelease(outCGImage);
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
        imageView.image = blurImage;
        [self.view addSubview:imageView];
    //方案二:利用UIImage + ImageEffects 分類   下載地址:https://github.com/YouXianMing/UIImageBlur
    
    UIImage *sourceImage = [UIImage imageNamed:@"1_1280x800"];
    
    //設置模糊度
    UIImage *blurImage = [sourceImage blurImageWithRadius:10];
    
    //設置局部模糊度
    //UIImage * blurImage2 = [image blurImageAtFrame:CGRectMake(0, 0, image.size.width, image.size.height/2)];
    
    //設置灰度效果
    //UIImage * blurImage3 = [image grayScale];
    
    /*其他的一些方法,可以自己嘗試使用
     固定寬度與固定高度
     - (UIImage *)scaleWithFixedWidth:(CGFloat)width;
     - (UIImage *)scaleWithFixedHeight:(CGFloat)height;
     平均的顏色
     - (UIColor *)averageColor;
     裁剪圖片的一部分
     - (UIImage *)croppedImageAtFrame:(CGRect)frame;
     */
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    imageView.image = blurImage;
    [self.view addSubview:imageView];
 //方案三:利用UIVisualEffectView(iOS8)
    // 添加展示的背景,用於顯示動態模糊(背景能夠滾動,便於查看動態的模糊)
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1_1280x800"]];
    self.scrollView.contentSize = imageV.image.size;
    self.scrollView.bounces = NO;
    [self.scrollView addSubview:imageV];
    [self.view addSubview:self.scrollView];
    
    //添加模糊效果
    // 1.創建模糊view
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    
    // 2.設定模糊View的尺寸
    effectView.frame = CGRectMake(0, 100, 375, 200);
    
    // 3.添加到view當中
    [self.view addSubview:effectView];
    
    //添加顯示文本
    UILabel *label = [[UILabel alloc] initWithFrame:effectView.bounds];
    label.text = @"模糊效果";
    label.font = [UIFont systemFontOfSize:40];
    label.textAlignment = NSTextAlignmentCenter;
    
    //添加模糊效果的子view
    // 1.創建出子模糊view
    UIVisualEffectView *subEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIVibrancyEffect effectForBlurEffect:(UIBlurEffect *)effectView.effect]];
    
    // 2.設置子模糊view的尺寸
    subEffectView.frame = effectView.bounds;
    
    // 3.將子模糊view添加到effectView的contentView上才能顯示
    [effectView.contentView addSubview:subEffectView];
    
    // 4.添加要顯示的view來達到特殊效果
    [subEffectView.contentView addSubview:label];



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章