mirror of
https://github.com/zoriya/react-native-svg.git
synced 2026-06-03 15:14:18 +00:00
feat: add macos back to Example app (#2119)
PR restoring macos CI job and restoring usage of UIGraphicsBeginImageContextWithOptions on macos since there is no implementation for UIGraphicsImageRendererFormat there yet.
This commit is contained in:
@@ -323,18 +323,27 @@ using namespace facebook::react;
|
||||
|
||||
- (NSString *)getDataURLWithBounds:(CGRect)bounds
|
||||
{
|
||||
#if !TARGET_OS_OSX // [macOS]
|
||||
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:bounds.size];
|
||||
|
||||
UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext *_Nonnull rendererContext) {
|
||||
#else // [macOS
|
||||
UIGraphicsBeginImageContextWithOptions(bounds.size, NO, 1);
|
||||
#endif // macOS]
|
||||
[self clearChildCache];
|
||||
[self drawRect:bounds];
|
||||
[self clearChildCache];
|
||||
[self invalidate];
|
||||
#if !TARGET_OS_OSX // [macOS]
|
||||
}];
|
||||
|
||||
#endif
|
||||
#if !TARGET_OS_OSX // [macOS]
|
||||
NSData *imageData = UIImagePNGRepresentation(image);
|
||||
NSString *base64 = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
|
||||
|
||||
#else // [macOS
|
||||
NSData *imageData = UIImagePNGRepresentation(UIGraphicsGetImageFromCurrentImageContext());
|
||||
NSString *base64 = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
|
||||
UIGraphicsEndImageContext();
|
||||
#endif // macOS]
|
||||
return base64;
|
||||
}
|
||||
|
||||
|
||||
@@ -304,6 +304,7 @@ UInt32 saturate(CGFloat value)
|
||||
CGContextRelease(bcontext);
|
||||
free(pixels);
|
||||
|
||||
#if !TARGET_OS_OSX // [macOS]
|
||||
UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat defaultFormat];
|
||||
format.scale = scale;
|
||||
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:boundsSize format:format];
|
||||
@@ -328,6 +329,37 @@ UInt32 saturate(CGFloat value)
|
||||
|
||||
// Render blended result into current render context
|
||||
CGImageRelease(maskImage);
|
||||
#else // [macOS
|
||||
// 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();
|
||||
|
||||
// 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);
|
||||
|
||||
CGContextSetBlendMode(newContext, kCGBlendModeSourceIn);
|
||||
CGContextDrawImage(newContext, drawBounds, contentImage);
|
||||
CGImageRelease(contentImage);
|
||||
|
||||
CGImageRef blendedImage = CGBitmapContextCreateImage(newContext);
|
||||
UIGraphicsEndImageContext();
|
||||
|
||||
// Render blended result into current render context
|
||||
CGContextDrawImage(context, drawBounds, blendedImage);
|
||||
CGImageRelease(blendedImage);
|
||||
#endif // macOS]
|
||||
} else {
|
||||
[self renderLayerTo:context rect:rect];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user