ios - Convert between UIImage and Base64 string -


does know how convert uiimage base64 string, , reverse it?

i have below code; original image before encoding good, blank image after encode , decode it.

nsdata *imagedata = uiimagepngrepresentation(viewimage);  nsstring *b64encstr = [self encode: imagedata];  nsstring *base64string = [self encodebase64:imagedata]; 

swift

first need have image's nsdata

//use image name bundle create nsdata let image : uiimage = uiimage(named:"imagenamehere")! //now use image create nsdata format let imagedata:nsdata = uiimagepngrepresentation(image)!  //or next possibility  //use image's path create nsdata let url:nsurl = nsurl(string : "urlhere")! //now use image create nsdata format let imagedata:nsdata = nsdata.init(contentsofurl: url)! 

swift 2.0 > encoding

let strbase64:string = imagedata.base64encodedstringwithoptions(.encoding64characterlinelength) 

swift 2.0 > decoding

let datadecoded:nsdata = nsdata(base64encodedstring: strbase64, options: nsdatabase64decodingoptions.ignoreunknowncharacters)! 

swift 3.0 > decoding

let datadecoded : data = data(base64encoded: strbase64, options: .ignoreunknowncharacters)! 

encoding :

let strbase64 = imagedata.base64encodedstring(options: .linelength64characters) print(strbase64) 

decoding :

let datadecoded:nsdata = nsdata(base64encodedstring: strbase64, options: nsdatabase64decodingoptions(rawvalue: 0))! let decodedimage:uiimage = uiimage(data: datadecoded)! print(decodedimage) yourimageview.image = decodedimage 

swift 3.0

let datadecoded : data = data(base64encoded: strbase64, options: .ignoreunknowncharacters)! let decodedimage = uiimage(data: datadecoded) yourimageview.image = decodedimage 

objective-c

ios7 > version

you can use nsdata's base64encodedstringwithoptions

encoding :

- (nsstring *)encodetobase64string:(uiimage *)image {  return [uiimagepngrepresentation(image) base64encodedstringwithoptions:nsdatabase64encoding64characterlinelength]; } 

decoding :

- (uiimage *)decodebase64toimage:(nsstring *)strencodedata {   nsdata *data = [[nsdata alloc]initwithbase64encodedstring:strencodedata options:nsdatabase64decodingignoreunknowncharacters];   return [uiimage imagewithdata:data]; } 

ios 6.1 , < version

first option : use this link encode , decode image

add base64 class in project.

encoding :

 nsdata* data = uiimagejpegrepresentation(yourimage, 1.0f);  nsstring *strencoded = [base64 encode:data]; 

decoding :

 nsdata* data = [base64 decode:strencoded ];;  image.image = [uiimage imagewithdata:data]; 

another option: use qsutilities encoding , decoding



Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -