Upgrade to Pro — share decks privately, control downloads, hide ads and more …

iOSでSVG画像を扱う

 iOSでSVG画像を扱う

Avatar for Kishikawa Katsumi

Kishikawa Katsumi

October 20, 2025
Tweet

More Decks by Kishikawa Katsumi

Other Decks in Programming

Transcript

  1. let path = "/System/Library/PrivateFrameworks/CoreSVG.framework/CoreSVG" guard let h = dlopen(path, RTLD_NOW)

    else { fatalError(String(cString: dlerror())) } J04Ͱ47(ը૾Λѻ͏ ϑϨʔϜϫʔΫΛಈతʹಡΈࠐΉ
  2. @objc class CGSVGDocument: NSObject {} typealias CreateFn = @convention(c) (CFData?,

    CFDictionary?) -> Unmanaged<CGSVGDocument>? guard let p = dlsym(h, "CGSVGDocumentCreateFromData") else { fatalError(String(cString: dlerror())) } let CGSVGDocumentCreateFromData = unsafeBitCast(p, to: CreateFn.self) J04Ͱ47(ը૾Λѻ͏ ؔ਺ͷγϯϘϧΛऔಘ͢Δ
  3. let sel = NSSelectorFromString("_imageWithCGSVGDocument:") typealias IMPFn = @convention(c) (AnyObject, Selector,

    CGSVGDocument) -> UIImage if let imp = (UIImage.self as AnyObject).method(for: sel) { let makeImage = unsafeBitCast(imp, to: IMPFn.self) let image = makeImage(UIImage.self, sel, document) ... } J04Ͱ47(ը૾Λѻ͏
  4. typealias DrawFn = @convention(c) (CGContext?, CGSVGDocument?) -> Void guard let

    p = dlsym(h, "CGContextDrawSVGDocument") else { fatalError(String(cString: dlerror())) } let CGContextDrawSVGDocument = unsafeBitCast(p, to: DrawFn.self) let size = CGSize(width: 191.19, height: 59.39) let render = UIGraphicsImageRenderer(size: size) let uiImage = render.image { (context) in let cgContext = context.cgContext cgContext.translateBy(x: 0, y: size.height) cgContext.scaleBy(x: 1, y: -1) CGContextDrawSVGDocument(cgContext, document) } J04Ͱ47(ը૾Λѻ͏
  5. J04Ͱ47(ը૾Λѻ͏ let path = URL(fileURLWithPath: "/System/Library/PrivateFrameworks/CoreSVG.framework") guard let cf =

    CFBundleCreate(kCFAllocatorDefault, path as CFURL) else { return } guard CFBundleLoadExecutable(cf) else { return } EMPQFOEMTZN#VOEMF"1*
  6. J04Ͱ47(ը૾Λѻ͏ @objc class CGSVGDocument: NSObject {} typealias CreateFn = @convention(c)

    (CFData?, CFDictionary?) -> Unmanaged<CGSVGDocument>? let p = CFBundleGetFunctionPointerForName(cf, "CGSVGDocumentCreateFromData" as CFString) guard let p else { return } let CGSVGDocumentCreateFromData: CreateFn = unsafeBitCast(p, to: CreateFn.self) EMPQFOEMTZN#VOEMF"1*
  7. J04Ͱ47(ը૾Λѻ͏ EMPQFOEMTZN#VOEMF"1* typealias DrawFn = @convention(c) (CGContext?, CGSVGDocument?) -> Void

    guard let p = dlsym( h, "CGContextDrawSVGDocument" ) else { fatalError(String(cString: dlerror())) } let CGContextDrawSVGDocument = unsafeBitCast(p, to: DrawFn.self)