Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Blur support Android & Ios. #993

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Ios platform add blur support.
  • Loading branch information
HyopeR committed Jul 3, 2023
commit 37c61a45d5e93f24eb156a3ff977be0a0ccd84a1
1 change: 1 addition & 0 deletions ios/FastImage/FFFastImageView.h
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
@property (nonatomic, strong) FFFastImageSource *source;
@property (nonatomic, strong) UIImage *defaultSource;
@property (nonatomic, strong) UIColor *imageColor;
@property (nonatomic, assign) CGFloat blurRadius;

@end

38 changes: 38 additions & 0 deletions ios/FastImage/FFFastImageView.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import "FFFastImageView.h"
#import <SDWebImage/UIImage+MultiFormat.h>
#import <SDWebImage/UIView+WebCache.h>
#import <CoreImage/CoreImage.h>

@interface FFFastImageView ()

@@ -71,6 +72,13 @@ - (void) setImageColor: (UIColor*)imageColor {
}
}

- (void)setBlurRadius:(CGFloat)blurRadius {
if (_blurRadius != blurRadius) {
_blurRadius = blurRadius;
_needsReload = YES;
}
}

- (UIImage*) makeImage: (UIImage*)image withTint: (UIColor*)color {
UIImage* newImage = [image imageWithRenderingMode: UIImageRenderingModeAlwaysTemplate];
UIGraphicsBeginImageContextWithOptions(image.size, NO, newImage.scale);
@@ -82,6 +90,13 @@ - (UIImage*) makeImage: (UIImage*)image withTint: (UIColor*)color {
}

- (void) setImage: (UIImage*)image {
if (_blurRadius && _blurRadius > 0) {
UIImage *blurImage = [self blurImage: image withRadius: _blurRadius];
if (blurImage) {
image = blurImage;
}
}

if (self.imageColor != nil) {
super.image = [self makeImage: image withTint: self.imageColor];
} else {
@@ -237,6 +252,29 @@ - (void) downloadImage: (FFFastImageSource*)source options: (SDWebImageOptions)o
}];
}

- (UIImage *)blurImage:(UIImage *)image withRadius:(CGFloat)radius {
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *inputImage = [CIImage imageWithCGImage:image.CGImage];

CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
[filter setValue:inputImage forKey:kCIInputImageKey];
[filter setValue:[NSNumber numberWithFloat:radius] forKey:kCIInputRadiusKey];
CIImage *outputImage = [filter valueForKey:kCIOutputImageKey];

if (outputImage) {
CGRect rect = CGRectMake(radius * 2, radius * 2, image.size.width - radius * 4, image.size.height - radius * 4);
CGImageRef outputImageRef = [context createCGImage:outputImage fromRect:rect];

if (outputImageRef) {
UIImage *blurImage = [UIImage imageWithCGImage:outputImageRef];
CGImageRelease(outputImageRef);
return blurImage;
}
}

return nil;
}

- (void) dealloc {
[self sd_cancelCurrentImageLoad];
}
1 change: 1 addition & 0 deletions ios/FastImage/FFFastImageViewManager.m
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ - (FFFastImageView*)view {
RCT_EXPORT_VIEW_PROPERTY(onFastImageLoad, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onFastImageLoadEnd, RCTDirectEventBlock)
RCT_REMAP_VIEW_PROPERTY(tintColor, imageColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(blurRadius, CGFloat)

RCT_EXPORT_METHOD(preload:(nonnull NSArray<FFFastImageSource *> *)sources)
{