【问题标题】:Blur View works in Simulator but not on device模糊视图适用于模拟器,但不适用于设备
【发布时间】:2015-10-25 21:44:24
【问题描述】:

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

【问题讨论】:

  • 你试过iPad模拟器吗?它是否按预期工作?
  • 您确定将模糊视图的子视图添加到模糊视图的 contentView 属性吗?如果您不在这里添加子视图,我相信该行为是未定义的。我需要查看屏幕截图才能更好地了解正在发生的事情。
  • 不,它在 iPad 模拟器中也不起作用。不知道该怎么办。示例(灰色部分是模糊视图):dropbox.com/s/e77uinulbqjmu81/…

标签: ios swift ios8 uivisualeffectview


【解决方案1】:

你用的是什么代码?

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
        }

【讨论】:

  • 是的,我使用这部分代码,但样式是Light 而不是Dark
  • 我用 iPhone 6+ (iOS 8.4) 检查了这段代码。它运作良好。
  • 是的,但请在 iPad 2 iOS 8.4 上尝试。它不会起作用。
【解决方案2】:

我也遇到了这个问题,并最终使用 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 添加到您的项目框架中。

【讨论】:

    猜你喜欢
    • 2012-11-01
    • 2011-03-08
    • 2015-02-13
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多