I wanted to know why my code runs much slower when I use dispatch_async compared to when I don't use it at all. I'm trying to blur the edges of my UIImage by masking it and using UIGraphicsImageRenderer (Not sure if it's the most efficient way or not..)But when I don't use dispatch_async, It runs much much faster. Why is that? Here is my code and the result I get from my code. Any help is muchhhhh appreciated.
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];
});
});