模糊视图在模拟器中工作,但在设备上不起作用

时间:2015-10-25 21:44:25

标签: ios swift ios8 uivisualeffectview

我在我的应用中添加了模糊视图。如果我在模拟器(iPhone 6,iOS 8.4)中运行应用程序,一切正常,并显示模糊视图。如果我在设备(iPad第3代,iOS 8.4)上运行应用程序,则会显示模糊视图,但不会显示模糊的背景,而是显示灰色背景。不知道为什么。你知道为什么以及如何解决它吗?

2 个答案:

答案 0 :(得分:0)

您使用的代码是什么?

func BlurEffect(){
     let extraLightBlur = UIBlurEffect(style: .Dark)
            let extraLightBlurView = UIVisualEffectView(effect: extraLightBlur)
            self.addSubview(extraLightBlurView)

            //let blurAreaAmount = CGRectMake(0, 0, frame.size.width, frame.size.height)
            extraLightBlurView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height)
            let extraLightVibrancyView = vibrancyEffectView(forBlurEffectView: extraLightBlurView)
            extraLightBlurView.contentView.addSubview(extraLightVibrancyView)
        }

        private func vibrancyEffectView(forBlurEffectView blurEffectView:UIVisualEffectView) -> UIVisualEffectView {
            let vibrancy = UIVibrancyEffect(forBlurEffect: blurEffectView.effect as! UIBlurEffect)
            let vibrancyView = UIVisualEffectView(effect: vibrancy)
            vibrancyView.frame = blurEffectView.frame
            //vibrancyView.autoresizingMask = .FlexibleWidth | .FlexibleHeight
            return vibrancyView
        }

答案 1 :(得分:0)

我也遇到了这个问题,并使用Core Image创建了一个UIView类别。它适用于非常古老的iPad mini和iPod touch。

标题

//
//  UIView+CIFilters.h
//

#import <UIKit/UIKit.h>
@interface UIView (CIFilters)
- (UIImageView *)blurredBackgroundViewWithRadius:(CGFloat)radius;
@end

类别

#import "UIView+CIFilters.h"

@implementation UIView (CIFilters)

- (UIImageView *)blurredBackgroundViewWithRadius:(CGFloat)radius {

    UIView *snapshot = self;
    // Get a CIImage from the view's contents
    UIGraphicsBeginImageContextWithOptions(snapshot.bounds.size, snapshot.opaque, 0);
    [snapshot drawViewHierarchyInRect:snapshot.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CIImage *snapshotImage = [CIImage imageWithCGImage:image.CGImage];

    //  Create a filter.
    //  This has to be applied first so that the edges of the CIGaussianBlur reach to the edge of the screen
    //  See the Core Image Filter Reference
    CIFilter *clampFilter = [CIFilter filterWithName:@"CIAffineClamp"
                                 withInputParameters:@{
                                                       kCIInputImageKey: snapshotImage
                                                       }];

    CIImage *clampImage = [clampFilter valueForKey:kCIOutputImageKey];

    CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"
                                withInputParameters:@{
                                                      kCIInputImageKey: clampImage,
                                                      kCIInputRadiusKey:@(radius)
                                                      }
                            ];
    CIImage *blurredCIImage = [blurFilter valueForKey: kCIOutputImageKey];

    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef cgImage = [context createCGImage:blurredCIImage fromRect:[snapshotImage extent]];

    UIImage *blurredImage = [UIImage imageWithCGImage:cgImage];
    UIImageView *blurredSnaphotview = [[UIImageView alloc] initWithImage:blurredImage];

    return blurredSnaphotview;
}

@end

例如

UIImageView *blurredSnapshot = [self.view blurredBackgroundViewWithRadius:10.0];

将生成self.view的整个视图层次结构的模糊快照,其中self是当前视图控制器。半径越大,图像越模糊。将此视图替换为UIVisualEffectView。

请务必将CoreImage添加到项目框架中。