簡體   English   中英

模糊視圖可在模擬器中使用,但不能在設備上使用

[英]Blur View works in Simulator but not on device

我在應用程序中添加了模糊視圖。 如果我在模擬器(iPhone 6,iOS 8.4)中運行該應用程序,則一切正常,並顯示模糊視圖。 如果我在設備(iPad 3rd Gen.,iOS 8.4)上運行該應用程序,則顯示模糊視圖,但不顯示模糊背景,而是顯示灰色背景。 不知道為什么。 您知道為什么以及如何解決嗎?

您正在使用什么代碼?

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
        }

我也遇到了這個問題,最后使用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添加到您的項目框架中。

暫無
暫無

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

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