ios - NSDataBase64DecodingOptions always returns nil -
there lot of similar questions non of them helped me out don't know ask. how encode uiimage:
let data: nsdata = uiimagepngrepresentation(imageresized)! let base64string: nsstring = data.base64encodedstringwithoptions(.encoding64characterlinelength)
and decoding (as suggested here):
if let range = base64.rangeofstring("data:image/png;base64,", options: .anchoredsearch) { base64.removerange(range) } let decodeddata = nsdata(base64encodedstring: base64, options: nsdatabase64decodingoptions(rawvalue: 0)) if let decodedimage = uiimage(data: decodeddata!) { self.imagelist.append(decodedimage) }
but app crashes when initializing decodeddata , can't figure out why. checked base64 string here , returns picture. appreciated!
try this:
if let decodeddata = nsdata(base64encodedstring: base64, options:nsdatabase64decodingoptions.ignoreunknowncharacters){ if(decodeddata.length > 0){ let imagedecodedata = uiimage(data: decodeddata)! } else{ print("error") } }
Comments
Post a Comment