@@ -701,15 +701,8 @@ - (nullable UIImage *)sd_blurredImageWithRadius:(CGFloat)blurRadius {
701
701
#endif
702
702
703
703
CGImageRef imageRef = self.CGImage ;
704
-
705
- // convert to BGRA if it isn't
706
- if (CGImageGetBitsPerPixel (imageRef) != 32 ||
707
- CGImageGetBitsPerComponent (imageRef) != 8 ||
708
- !((CGImageGetBitmapInfo (imageRef) & kCGBitmapAlphaInfoMask ))) {
709
- SDGraphicsBeginImageContextWithOptions (self.size , NO , self.scale );
710
- [self drawInRect: CGRectMake (0 , 0 , self .size.width, self .size.height)];
711
- imageRef = SDGraphicsGetImageFromCurrentImageContext ().CGImage ;
712
- SDGraphicsEndImageContext ();
704
+ if (!imageRef) {
705
+ return nil ;
713
706
}
714
707
715
708
vImage_Buffer effect = {}, scratch = {};
@@ -726,7 +719,7 @@ - (nullable UIImage *)sd_blurredImageWithRadius:(CGFloat)blurRadius {
726
719
};
727
720
728
721
vImage_Error err;
729
- err = vImageBuffer_InitWithCGImage (&effect, &format, NULL , imageRef, kvImageNoFlags);
722
+ err = vImageBuffer_InitWithCGImage (&effect, &format, NULL , imageRef, kvImageNoFlags); // vImage will convert to format we requests, no need `vImageConvert`
730
723
if (err != kvImageNoError) {
731
724
NSLog (@" UIImage+Transform error: vImageBuffer_InitWithCGImage returned error code %zi for inputImage: %@ " , err, self);
732
725
return nil ;
@@ -740,6 +733,7 @@ - (nullable UIImage *)sd_blurredImageWithRadius:(CGFloat)blurRadius {
740
733
input = &effect;
741
734
output = &scratch;
742
735
736
+ // See: https://developer.apple.com/library/archive/samplecode/UIImageEffects/Introduction/Intro.html
743
737
if (hasBlur) {
744
738
// A description of how to compute the box kernel width from the Gaussian
745
739
// radius (aka standard deviation) appears in the SVG spec:
@@ -756,19 +750,16 @@ - (nullable UIImage *)sd_blurredImageWithRadius:(CGFloat)blurRadius {
756
750
if (inputRadius - 2.0 < __FLT_EPSILON__) inputRadius = 2.0 ;
757
751
uint32_t radius = floor (inputRadius * 3.0 * sqrt (2 * M_PI) / 4 + 0.5 );
758
752
radius |= 1 ; // force radius to be odd so that the three box-blur methodology works.
759
- int iterations;
760
- if (blurRadius * scale < 0.5 ) iterations = 1 ;
761
- else if (blurRadius * scale < 1.5 ) iterations = 2 ;
762
- else iterations = 3 ;
763
753
NSInteger tempSize = vImageBoxConvolve_ARGB8888 (input, output, NULL , 0 , 0 , radius, radius, NULL , kvImageGetTempBufferSize | kvImageEdgeExtend);
764
754
void *temp = malloc (tempSize);
765
- for (int i = 0 ; i < iterations; i++) {
766
- vImageBoxConvolve_ARGB8888 (input, output, temp, 0 , 0 , radius, radius, NULL , kvImageEdgeExtend);
767
- vImage_Buffer *tmp = input;
768
- input = output;
769
- output = tmp;
770
- }
755
+ vImageBoxConvolve_ARGB8888 (input, output, temp, 0 , 0 , radius, radius, NULL , kvImageEdgeExtend);
756
+ vImageBoxConvolve_ARGB8888 (output, input, temp, 0 , 0 , radius, radius, NULL , kvImageEdgeExtend);
757
+ vImageBoxConvolve_ARGB8888 (input, output, temp, 0 , 0 , radius, radius, NULL , kvImageEdgeExtend);
771
758
free (temp);
759
+
760
+ vImage_Buffer *tmp = input;
761
+ input = output;
762
+ output = tmp;
772
763
}
773
764
774
765
CGImageRef effectCGImage = NULL ;
0 commit comments