在新iOS的音乐应用程序中,我们可以看到一个模糊的视图后面有一个专辑封面。
这样的事情是如何完成的呢?我看了文件,但什么都没发现。
在新iOS的音乐应用程序中,我们可以看到一个模糊的视图后面有一个专辑封面。
这样的事情是如何完成的呢?我看了文件,但什么都没发现。
当前回答
我认为最简单的解决方案是覆盖UIToolbar,它在iOS 7中模糊了它后面的所有东西。这很狡猾,但实现起来非常简单,而且速度很快!
你可以对任何视图这么做,只是让它成为UIToolbar的子类而不是UIView。你甚至可以用UIViewController的view属性来做,例如…
1)创建一个新类,它是UIViewController的“子类”,并选中“With XIB for user interface”。
2)选择View,进入右边面板中的标识检查器(alt-command-3)。将“Class”改为UIToolbar。现在转到属性检查器(alt-command-4),将“背景”颜色改为“清色”。
3)在主视图中添加子视图,并将其连接到接口中的IBOutlet。叫它backgroundColorView。它看起来像这样,是实现(.m)文件中的一个私有类别。
@interface BlurExampleViewController ()
@property (weak, nonatomic) IBOutlet UIView *backgroundColorView;
@end
4)转到视图控制器实现(.m)文件并更改-viewDidLoad方法,如下所示:
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.barStyle = UIBarStyleBlack; // this will give a black blur as in the original post
self.backgroundColorView.opaque = NO;
self.backgroundColorView.alpha = 0.5;
self.backgroundColorView.backgroundColor = [UIColor colorWithWhite:0.3 alpha:1];
}
这将给你一个深灰色的视图,它模糊了它后面的一切。没有有趣的业务,没有缓慢的核心图像模糊,使用操作系统/SDK提供的一切。
你可以将这个视图控制器的视图添加到另一个视图,如下所示:
[self addChildViewController:self.blurViewController];
[self.view addSubview:self.blurViewController.view];
[self.blurViewController didMoveToParentViewController:self];
// animate the self.blurViewController into view
如果有什么不清楚的地方请告诉我,我很乐意帮忙!
Edit
UIToolbar在7.0.3中被改变,当使用彩色模糊时可能会产生不希望看到的效果。
我们过去能够使用barTintColor设置颜色,但如果你以前这样做,你将需要将alpha组件设置为小于1。否则你的UIToolbar将是完全不透明的颜色-没有模糊。
这可以通过如下方式实现:(记住self是UIToolbar的子类)
UIColor *color = [UIColor blueColor]; // for example
self.barTintColor = [color colorWithAlphaComponent:0.5];
这将给模糊的视图一个蓝色的色调。
其他回答
使用UIImageEffects
对于那些想要更多控制权的人,你可以使用苹果的UIImageEffects示例代码。
你可以从苹果的开发者库中复制UIImageEffects的代码:模糊和着色图像
下面是如何应用它:
#import "UIImageEffects.h"
...
self.originalImageView.image = [UIImageEffects imageByApplyingLightEffectToImage:[UIImage imageNamed:@"yourImage.png"]];
自定义模糊比例
你可以尝试UIVisualEffectView自定义设置为-
class BlurViewController: UIViewController {
private let blurEffect = (NSClassFromString("_UICustomBlurEffect") as! UIBlurEffect.Type).init()
override func viewDidLoad() {
super.viewDidLoad()
let blurView = UIVisualEffectView(frame: UIScreen.main.bounds)
blurEffect.setValue(1, forKeyPath: "blurRadius")
blurView.effect = blurEffect
view.addSubview(blurView)
}
}
输出:- for blureeffect . setvalue(1…& blurEffect.setValue (2 . .
func blurBackgroundUsingImage(image: UIImage)
{
var frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
var imageView = UIImageView(frame: frame)
imageView.image = image
imageView.contentMode = .ScaleAspectFill
var blurEffect = UIBlurEffect(style: .Light)
var blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = frame
var transparentWhiteView = UIView(frame: frame)
transparentWhiteView.backgroundColor = UIColor(white: 1.0, alpha: 0.30)
var viewsArray = [imageView, blurEffectView, transparentWhiteView]
for index in 0..<viewsArray.count {
if let oldView = self.view.viewWithTag(index + 1) {
var oldView = self.view.viewWithTag(index + 1)
// Must explicitly unwrap oldView to access its removeFromSuperview() method as of Xcode 6 Beta 5
oldView!.removeFromSuperview()
}
var viewToInsert = viewsArray[index]
self.view.insertSubview(viewToInsert, atIndex: index + 1)
viewToInsert.tag = index + 1
}
}
Swift 3版本的凯文的答案返回模糊的图像-
func blurBgImage(image: UIImage) -> UIImage? {
let radius: CGFloat = 20;
let context = CIContext(options: nil);
let inputImage = CIImage(cgImage: image.cgImage!);
let filter = CIFilter(name: "CIGaussianBlur");
filter?.setValue(inputImage, forKey: kCIInputImageKey);
filter?.setValue("\(radius)", forKey:kCIInputRadiusKey);
if let result = filter?.value(forKey: kCIOutputImageKey) as? CIImage{
let rect = CGRect(origin: CGPoint(x: radius * 2,y :radius * 2), size: CGSize(width: image.size.width - radius * 4, height: image.size.height - radius * 4))
if let cgImage = context.createCGImage(result, from: rect){
return UIImage(cgImage: cgImage);
}
}
return nil;
}
我不认为我被允许发布代码,但上面提到的WWDC示例代码是正确的。链接如下:https://developer.apple.com/downloads/index.action?name=WWDC%202013
你要找的文件是UIImage上的category,方法是applyLightEffect。
正如我在上面的评论中提到的,除了模糊,苹果模糊还有饱和度和其他东西。简单的模糊是不行的……如果你想模仿他们的风格。