ios - 为什么使用 dispatch_async 比完全不使用它要慢?

标签 ios queue calayer cashapelayer dispatch-async

我想知道为什么我的代码在使用 dispatch_async 时比完全不使用它时运行得慢得多。我试图通过屏蔽它并使用 UIGraphicsImageRenderer 来模糊我的 UIImage 的边缘(不确定它是否是最有效的方法..)但是当我不使用 dispatch_async 时,它运行得更快。这是为什么?这是我的代码和我从代码中得到的结果。非常感谢任何帮助。

enter image description here

self.view.backgroundColor = [UIColor whiteColor];
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                UIImage* Img =  [UIImage imageNamed:@"1"];
                UIImageView * imageview = [[UIImageView alloc]initWithImage:Img];
                UIGraphicsImageRenderer * renderer = [[UIGraphicsImageRenderer alloc] initWithSize:Img.size];

                UIBezierPath*path=[UIBezierPath bezierPathWithRoundedRect:CGRectMake(20,20,170,170) cornerRadius:5.0];
                path.lineWidth = 20;

                CAShapeLayer*shapeLayer = [CAShapeLayer new];
                shapeLayer.path=path.CGPath;

                [shapeLayer setFillColor:[UIColor redColor].CGColor];
                [shapeLayer fillColor];

                UIImage *shapeImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull context){
                    [shapeLayer renderInContext: context.CGContext];}];

                CIImage * shapeCimage = [[CIImage alloc] initWithImage:shapeImage];
                CIFilter * gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
                [gaussianBlurFilter setValue:shapeCimage forKey: @"inputImage"];
                [gaussianBlurFilter setValue:@15 forKey:@"inputRadius"];

                CIImage * blurredCIImage = [gaussianBlurFilter valueForKey:kCIOutputImageKey];
                UIImage * blurredImage = [UIImage imageWithCIImage:blurredCIImage];

                UIImageView *maskedImageView = [[UIImageView alloc]initWithImage:blurredImage];
                maskedImageView.contentMode = UIViewContentModeScaleAspectFit;
                maskedImageView.frame = imageview.frame;
                imageview.layer.mask=maskedImageView.layer;

                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.view addSubview:imageview];
                });
            });

最佳答案

所有 UI 的东西都应该在主队列上执行。否则,这可能会导致错误(不时)和令人难以置信的缓慢性能。

这是因为 UIApplication 是在主线程上设置的。

你可以看看这个教程:

  1. http://blog.grio.com/2014/04/understanding-the-ios-main-thread.html

  2. https://developer.apple.com/library/content/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW15 .

最好的问候

关于ios - 为什么使用 dispatch_async 比完全不使用它要慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43712029/

相关文章:

ios - Interface Builder 文件中的未知类 ViewController

java - Mysql中的队列系统

c# - 非泛型类型 'System.Collections.IEnumerable' 不能与类型参数一起使用

ios - CAShapeLayer subview 未正确设置动画

ios - 如何初始化代码在 Swift 4 中生成的按钮边框

ios - 使用cornerRadius实现的圆形UIView看起来是 block 状的

ios - view.removeFromSuperview() 很多水龙头的 fatal error

ios - UIView Shadow 使用 BezierPath 大量偏移阴影

ios - 将 UIImage 转换为 NSData 并与核心数据一起保存

c++ - 如何在 C++ 中使用堆栈、队列或 BST 对文件中的记录进行排序?