(ONLY iOS/Mac OS X)
I found seems to be a beautiful way on how to get Image from SVG.
The main idea is to use Apple’s PDFKit framework and .AI (Adobe Illustrator) or .Pdf format.
1 Rule: If you are using .AI (Adobe Illustrator format) then during “Save As” Make sure you are checked “Create PDF Compatible File”.
2 Rule If you are using PDF, make sure to save your PDF as vector, i.e. not raster. For this during export select “Adobe PDF Preset”: Illustrator Default.
Into your C++ project add:
PDFHandler.h
#ifndef PDFHANDLER_H
#define PDFHANDLER_H
#include <juce_graphics/juce_graphics.h>
juce::Image pdfToJuceImage(const char* data, int dataSize, int width, int height);
#endif // PDFHANDLER_H
PDFHandler.mm
#import "PDFHandler.h"
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <PDFKit/PDFKit.h>
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#elif TARGET_OS_MAC
#import <AppKit/AppKit.h>
#endif
#endif
juce::Image pdfToJuceImage(const char* data, int dataSize, int width, int height)
{
@autoreleasepool
{
// Create NSData from the provided data
NSData* pdfData = [NSData dataWithBytes:data length:(NSUInteger)dataSize];
// Create PDFDocument from NSData
PDFDocument* pdfDocument = [[PDFDocument alloc] initWithData: pdfData];
if (pdfDocument == nil || [pdfDocument pageCount] == 0) {
return juce::Image();
}
// Always use the first page
PDFPage* page = [pdfDocument pageAtIndex: 0];
if (page == nil) {
return juce::Image();
}
// Get the bounds of the PDF page
CGRect pageRect = [page boundsForBox: kPDFDisplayBoxMediaBox];
#if TARGET_OS_IPHONE
// iOS: Use UIGraphicsBeginImageContextWithOptions
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
// Set transparent background
CGContextClearRect(context, CGRectMake(0, 0, width, height));
// Scale the context to fit the bounds
CGContextTranslateCTM(context, 0.0, height);
CGContextScaleCTM(context, width / pageRect.size.width, -height / pageRect.size.height);
// Set Quality
CGContextSetAllowsAntialiasing(context, true);
CGContextSetShouldAntialias(context, true);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
// Draw the PDF page onto the context
[page drawWithBox: kPDFDisplayBoxMediaBox toContext: context];
// Get UIImage from the current image context
UIImage* uiImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (uiImage == nil) {
return juce::Image();
}
// Convert UIImage into juce::Image
NSData* imageData = UIImagePNGRepresentation(uiImage);
juce::MemoryInputStream stream(imageData.bytes, imageData.length, false);
return juce::ImageFileFormat::loadFrom(stream);
#elif TARGET_OS_MAC
// macOS: Use NSImage and NSGraphicsContext
NSRect rect = NSMakeRect(0, 0, width, height);
NSImage* nsImage = [[NSImage alloc] initWithSize:rect.size];
[nsImage lockFocus];
CGContextRef context = [NSGraphicsContext currentContext].CGContext;
// Set transparent background
CGContextClearRect(context, CGRectMake(0, 0, width, height));
// Scale the context to fit the bounds
CGContextScaleCTM(context, width / pageRect.size.width, height / pageRect.size.height);
// Set Quality
CGContextSetAllowsAntialiasing(context, true);
CGContextSetShouldAntialias(context, true);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
// Draw the PDF page onto the context
[page drawWithBox: kPDFDisplayBoxMediaBox toContext: context];
[nsImage unlockFocus];
if (nsImage == nil) {
return juce::Image();
}
// Convert NSImage to NSData
CGImageRef cgImage = [nsImage CGImageForProposedRect:NULL context:nil hints:nil];
NSBitmapImageRep* bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
NSData* imageData = [bitmapRep representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
// Convert NSData into juce::Image
juce::MemoryInputStream stream([imageData bytes], [imageData length], false);
return juce::ImageFileFormat::loadFrom(stream);
#endif
}
}
Projucer
Make sure you added PDFKit.framework (see Extra System Frameworks )
Cmake (optional)
# Locate the PDFKit framework
find_library(PDFKIT_FRAMEWORK PDFKit)
...
target_link_libraries(YourSuperPooperApp
PRIVATE
GuiAppData
juce::juce_gui_extra
juce::juce_dsp
juce::juce_animation
juce::juce_audio_utils
${PDFKIT_FRAMEWORK} #### <------ ADD THIS
PUBLIC
juce::juce_recommended_config_flags
juce::juce_recommended_lto_flags
juce::juce_recommended_warning_flags)
Usage:
In your MainComponent.h for example add
#include "PDFHandler.h"
MainComponent() {
yourJuceImage = pdfToJuceImage(BinaryData::default_fader_cap_ai, BinaryData::default_fader_cap_aiSize, width, height);
}
[NOTE]: Keep in mind that image will be returned 2X larger if you are using retina display. So if you don’t want that, set scale to UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, 1.0);
As you can see I’m using BinaryData to get the ai file from resources. The code is not optimized, but you get this beautiful and clean juce::Image, which support Everething! Shadows, Filters, Gradients and more, more 
I’ll be glad if someone helps optimize it, I haven’t had time for it yet, but I’ll share it with you. Thanks!