Decode base64_encode Image from JSON in Swift -
i have mysql database contains images. receive data php file:
php: $result[$key]['image'] = based64_encode($resultarray[$key]['image']);
now json file, this:
json: {"image":"\/9j\/4q\/+rxhpzgaatu0akgaaaagacgepaaiaaaagaaaahgeqaaiaaaakaaaajaesaamaaaabaayaaaeaaauaaaabaaaalgebaauaaaabaaaangeoaamaaaabaaiaae...
i have swift project , want decode image uiimage, far have no idea how decode image. have following.
swift: alamofire.request(.get, url).responsejson { (response) -> void in if let json = response.result.value as? [[string : anyobject]]{ json in json{ json let encodedimage = json["image"] let imagedata = nsdata(base64encodedstring: encodedimage) } }
how can decode image can display it?
you have cast dictionary value anyobject string. have decode string data using .ignoreunknowncharacters option. try this
if let encodedimage = json["image"] as? string, imagedata = nsdata(base64encodedstring: encodedimage, options: .ignoreunknowncharacters), image = uiimage(data: imagedata) { print(image.size) }
swift 3.0.1 • xcode 8.1
if if let encodedimage = json["image"] as? string, let imagedata = data(base64encoded: encodedimage, options: .ignoreunknowncharacters), let image = uiimage(data: imagedata) { print(image.size) }
Comments
Post a Comment