ZhgChgLi/ZMarkupParser

base64 images

JodliDev opened this issue · 3 comments

Hello. Thank you very much for this library! It makes handling HTML so much easier!

One question: Is there any chance that it will get base64-image support? Or could you give me some pointers on how to add an according tag-handling system?

When looking through the source, I noticed that this

let attachment = ZNSTextAttachment(imageURL: srcURL, imageWidth: width, imageHeight: height, placeholderImage: placeholderImage(size: CGSize(width: width ?? 50, height: height ?? 50)))
is using an UIImage for the placeholder. So it should be possible to just replace the ZNSTextAttachment-code somewhere down the line with just something like

let regex = try NSRegularExpression(pattern: "data:image/.+;base64,(.+)")
let result = regex.firstMatch(in: url, options: [], range: NSRange(url.startIndex..., in: url))
let nsString = url as NSString

let data = Data(base64Encoded: nsString.substring(with: result!.range(at: 1)), options: .ignoreUnknownCharacters)
return UIImage(data: data!)

But I cannot figure out where.
I guess I would have to import ZNSTextAttachment and then replicate your code. But I could use some pointers on how to utitilize the ZHTMLParserBuilder.add-method for that

Thanks a lot! :)

will research and implement

Thank you for your hard work! :)

In the latest version 1.8.0, the issues have been addressed.
Thank you for your patience.