ios - How to convert base64 image embedded in XML to UIImage? -
first of need parse xml. on ios there no nsxml
, need use nsxmlparser
instead, primitive event-driven callback parser (opposed complex tree parser use in macos).
so let's define sample data:
let xmlstring = "<base64binary xmlns=\"testtesttest\">" + "/9j/4aaqskzjrgabaqeasabiaad/2wbdaa0jcgskca0lcgsodg0peyavexis" + "eycchhcglikxmc4plswzoko+mzzgnywtqfdbrkxoulnsmj5ayvpqyepruk//" + "2wbdaq4odhmreyyvfszpns01t09pt09pt09pt09pt09pt09pt09pt09pt09p" + "t09pt09pt09pt09pt09pt09pt09pt0//waarcabaabwdasiaahebaxeb/8qa" + "ggaaagmbaqaaaaaaaaaaaaaaawucbayaaf/eacwqaaibawmcbaufaaaaaaaa" + "aaecawaeequsitfbbijhcrmuuyghfsnckeh/xaaxaqebaqeaaaaaaaaaaaaa" + "aaadagqa/8qahreaagicaweaaaaaaaaaaaaaaaecerihayixmv/aaawdaqac" + "eqmrad8avwmqksvifbarkkz4+g9ajejbwmiljjmzn6ozezhrqi5jylol4ubf" + "jjlwnhoksyzxqssli4aundmd+rnplvlxzwy4rwkbguzdoujjqdsm5xrba4/n" + "n3ryxkmzexby/jzqpzix8r4fznj3oapo7lywreroxgsr0j/yluq6o1rglogx" + "lkn8moc0y0hwmlqnu3b5n69qz/ionwmygg4pibzits8rr49xlbl1aohbmlbw" + "dudnao3vrbdlisfxlwwpeu0lhc2ai+d6db+9wn6w5ma9brzltxsal6tmgt7a" + "3gbyg7nh0znmkgqxssxdq7qihqc9a0finyjlg1encfdyntwax8ypiyi0y3gy" + "3y6u1v6pd50ptijyqtmysgy5qhrt7e1nkloss3jplo8lvdaahur+7h5hi4yr" + "0p3fw/k7n+cfmpfe27aknyqvjiv0knpd543o30oazb2n2wnwhbhouzzj61qv" + "edlkbyxt3szklbpbfflqpccz96oykan3rssqdoaednhapfbl50kkifd1yzpz" + "o1ckag3anvmmlsogi+j/roph6xayavb3tpd85dbgqdqjsefc0+e1swxatm+u" + "7r+a7ctjm7z//9k=" + "</base64binary>"
now let's parse that. therefor need primitive delegate, this:
/// extracts content of first <bas64binary> tag in xml class base64binaryextractor: nsobject, xmlparserdelegate { private var active: bool = false var base64string: string? = nil func parser ( _ parser: xmlparser, didstartelement elementname: string, namespaceuri: string?, qualifiedname qname: string?, attributes attributedict: [string : string] = [:] ) { // must not have string yet guard self.base64string == nil else { // if do, second <bas64binary> tag or // it's tag within tag. ingore both. self.active = false return } // care "<base64binary>" guard elementname == "base64binary" else { return } // time collect string self.active = true self.base64string = "" } func parser ( _ parser: xmlparser, didendelement elementname: string, namespaceuri: string?, qualifiedname qname: string? ) { // no matter element, no matter if active, // not active anymore once tag ends. self.active = false } func parser ( _ parser: xmlparser, foundcharacters string: string ) { // ignore unless active guard self.active else { return } self.base64string!.append(string) } }
the actual parsing converting string data, setting parser, setting delegate , making run:
let xmldata = xmlstring.data(using: .utf8) // todo: check xmldata being nil , handle that! let parser = xmlparser(data: xmldata!) let delegate = base64binaryextractor() parser.delegate = delegate let parseok = parser.parse() // todo: check parseok , handle if not! // todo: check delegate.base64string being nil , handle!
now need decoding base64 raw data , make image out of it:
let rawdata = data(base64encoded: delegate.base64string!) // todo: check raw data being nil (bad base64!) , hanlde that! let image = uiimage(data: rawdata!)
the code above assumes rawdata
contains data of image format known ios (e.g. png, jpeg, tiff) if not case, need convert format, maybe using external image library process it.
and bonus, here 2 valuable tips future questions on stackoverflow:
always provide sample data. screenshot doesn't cut cannot use testing sample code. sample data helps people lot try you, more answers, better answers , them quicker if provide sample data.
break complex problems smaller sub-problems , ask one question per sub-problem, instead of creating complex "covers all" questions, increases chances fast , accurate replies dramatically. it's how should develop software: divide , conquer. convert large problems small sub-problems, solve 1 sub-problem @ time, if you've solved them all, you've solved complex problem whole, too. e.g. problem can broken down 3 sub-problems:
- parsing xml document extract content of specific tag
- converting base64 data raw data
- creating image raw data
Comments
Post a Comment