我在我的应用程序中添加了一个模糊视图。如果我在模拟器(iPhone 6、iOS 8.4)中运行该应用程序,一切正常并显示模糊视图。如果我在设备(iPad 第 3 代,iOS 8.4)上运行该应用程序,则会显示模糊视图,但不会显示模糊背景,而是显示灰色背景。不知道为什么。你知道为什么以及如何解决它吗?
问问题
441 次
2 回答
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
}
于 2015-10-27T07:47:02.137 回答
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 添加到您的项目框架中。
于 2016-09-11T17:45:17.877 回答