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

fix: remove usage of deprecated UIGraphicsBeginImageContextWithOptions #8

Open
wants to merge 1 commit into
base: exodus-fork
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion RNSVG.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Pod::Spec.new do |s|
s.license = package['license']
s.homepage = package['homepage']
s.authors = 'Horcrux Chen'
s.platforms = { :ios => "9.0", :tvos => "9.2" }
s.platforms = { :ios => "10.0", :tvos => "9.2" }
s.source = { :git => 'https://github.com/react-native-community/react-native-svg.git', :tag => "v#{s.version}" }
s.source_files = 'ios/**/*.{h,m}'
s.requires_arc = true
Expand Down
4 changes: 1 addition & 3 deletions ios/Elements/RNSVGSvgView.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@

- (RNSVGNode *)getDefinedMask:(NSString *)maskName;

- (NSString *)getDataURL;

- (NSString *)getDataURLwithBounds:(CGRect)bounds;
- (NSString *)getDataURLWithBounds:(CGRect)bounds;

- (CGRect)getContextBounds;

Expand Down
30 changes: 10 additions & 20 deletions ios/Elements/RNSVGSvgView.m
Original file line number Diff line number Diff line change
Expand Up @@ -253,29 +253,19 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
return nil;
}

- (NSString *)getDataURL
- (NSString *)getDataURLWithBounds:(CGRect)bounds
{
UIGraphicsBeginImageContextWithOptions(_boundingBox.size, NO, 0);
[self clearChildCache];
[self drawRect:_boundingBox];
[self clearChildCache];
[self invalidate];
NSData *imageData = UIImagePNGRepresentation(UIGraphicsGetImageFromCurrentImageContext());
NSString *base64 = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
UIGraphicsEndImageContext();
return base64;
}
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:bounds.size];

- (NSString *)getDataURLwithBounds:(CGRect)bounds
{
UIGraphicsBeginImageContextWithOptions(bounds.size, NO, 0);
[self clearChildCache];
[self drawRect:bounds];
[self clearChildCache];
[self invalidate];
NSData *imageData = UIImagePNGRepresentation(UIGraphicsGetImageFromCurrentImageContext());
UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext *_Nonnull rendererContext) {
[self clearChildCache];
[self drawRect:bounds];
[self clearChildCache];
[self invalidate];
}];

NSData *imageData = UIImagePNGRepresentation(image);
NSString *base64 = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
UIGraphicsEndImageContext();
return base64;
}

Expand Down
16 changes: 8 additions & 8 deletions ios/RNSVGNode.m
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,14 @@ - (CGPathRef)getClipPath:(CGContextRef)context
CGRect bounds = CGContextGetClipBoundingBox(context);
CGSize size = bounds.size;

UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
CGContextRef newContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(newContext, 0.0, size.height);
CGContextScaleCTM(newContext, 1.0, -1.0);

[_clipNode renderLayerTo:newContext rect:bounds];
_clipMask = CGBitmapContextCreateImage(newContext);
UIGraphicsEndImageContext();
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:size];
UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext *_Nonnull rendererContext) {
CGContextRef newContext = rendererContext.CGContext;
CGContextTranslateCTM(newContext, 0.0, size.height);
CGContextScaleCTM(newContext, 1.0, -1.0);
[_clipNode renderLayerTo:newContext rect:bounds];
}];
_clipMask = CGImageRetain(image.CGImage);
}
}

Expand Down
42 changes: 18 additions & 24 deletions ios/RNSVGRenderable.m
Original file line number Diff line number Diff line change
Expand Up @@ -242,35 +242,29 @@ - (void)renderTo:(CGContextRef)context rect:(CGRect)rect
CGContextRelease(bcontext);
free(pixels);

// Render content of current SVG Renderable to image
UIGraphicsBeginImageContextWithOptions(boundsSize, NO, 0.0);
CGContextRef newContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(newContext, 0.0, height);
CGContextScaleCTM(newContext, 1.0, -1.0);
[self renderLayerTo:newContext rect:rect];
CGImageRef contentImage = CGBitmapContextCreateImage(newContext);
UIGraphicsEndImageContext();
UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat defaultFormat];
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:boundsSize format:format];

// Blend current element and mask
UIGraphicsBeginImageContextWithOptions(boundsSize, NO, 0.0);
newContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(newContext, 0.0, height);
CGContextScaleCTM(newContext, 1.0, -1.0);

CGContextSetBlendMode(newContext, kCGBlendModeCopy);
CGContextDrawImage(newContext, drawBounds, maskImage);
CGImageRelease(maskImage);
// Get the content image
UIImage *contentImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext *_Nonnull rendererContext) {
CGContextTranslateCTM(rendererContext.CGContext, 0.0, height);
CGContextScaleCTM(rendererContext.CGContext, 1.0, -1.0);
[self renderLayerTo:rendererContext.CGContext rect:rect];
}];

CGContextSetBlendMode(newContext, kCGBlendModeSourceIn);
CGContextDrawImage(newContext, drawBounds, contentImage);
CGImageRelease(contentImage);
// Blend current element and mask
UIImage *blendedImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext *_Nonnull rendererContext) {
CGContextSetBlendMode(rendererContext.CGContext, kCGBlendModeCopy);
CGContextDrawImage(rendererContext.CGContext, drawBounds, maskImage);
CGContextSetBlendMode(rendererContext.CGContext, kCGBlendModeSourceIn);
CGContextDrawImage(rendererContext.CGContext, drawBounds, contentImage.CGImage);
}];

CGImageRef blendedImage = CGBitmapContextCreateImage(newContext);
UIGraphicsEndImageContext();
// Render blended result into current render context
[blendedImage drawInRect:drawBounds];

// Render blended result into current render context
CGContextDrawImage(context, drawBounds, blendedImage);
CGImageRelease(blendedImage);
CGImageRelease(maskImage);
} else {
[self renderLayerTo:context rect:rect];
}
Expand Down
4 changes: 2 additions & 2 deletions ios/ViewManagers/RNSVGSvgViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ - (void)toDataURL:(nonnull NSNumber *)reactTag options:(NSDictionary *)options c
if ([view isKindOfClass:[RNSVGSvgView class]]) {
RNSVGSvgView *svg = view;
if (options == nil) {
b64 = [svg getDataURL];
b64 = [svg getDataURLWithBounds:svg.boundingBox];
} else {
id width = [options objectForKey:@"width"];
id height = [options objectForKey:@"height"];
Expand All @@ -61,7 +61,7 @@ - (void)toDataURL:(nonnull NSNumber *)reactTag options:(NSDictionary *)options c
NSInteger hi = (NSInteger)[h intValue];

CGRect bounds = CGRectMake(0, 0, wi, hi);
b64 = [svg getDataURLwithBounds:bounds];
b64 = [svg getDataURLWithBounds:bounds];
}
} else {
RCTLogError(@"Invalid svg returned frin registry, expecting RNSVGSvgView, got: %@", view);
Expand Down