简体   繁体   中英

Blur View works in Simulator but not on device

I added a blur view in my app. If I run the app in the Simulator (iPhone 6, iOS 8.4) all works fine and the blur view is displayed. If I run the app on an device (iPad 3rd Gen., iOS 8.4) the blur view is displayed but doesn't displays a blurred background instead of this it displays a grey background. Don't know why. Do you know why and how to solve it?

What code you are using?

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
        }

I ran into this problem too and wound up rolling my own blur effect using Core Image to create a UIView category. It works on a very old iPad mini and an iPod touch.

The header

//
//  UIView+CIFilters.h
//

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

The category

#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

For example

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

will make a blurred snapshot of the entire view hierarchy of self.view where self is the current view controller. The larger the radius the blurrier the image. Substitute this view for a UIVisualEffectView.

Be sure to add CoreImage to your project frameworks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM