Picture 64-bit encoding conversion small note

Source: Internet
Author: User


Accepted
Swift

First we need to has image ' s NSData

Use image name from bundle to create NSDataLet image: UIImage = UIImage(Named:"Imagenamehere")!Now with the image to the create into NSData formatLet ImageData:NSData = Uiimagepngrepresentation(Image)! //or next possibility//use image ' s path to Create Nsdatalet url:nsurl =  Nsurl (string :  "Urlhere" )! //now use image to create into NSData formatlet 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)!

Encoding :

let strBase64 = imageData.base64EncodedStringWithOptions(.allZeros)print(strBase64)

decoding :

Let datadecoded:NSData = NSData(Base64encodedstring: Strbase64, Options: Span class= "Typ" >nsdatabase64decodingoptions (rawvalue:< Span class= "PLN" > 0)! let decodedimage:uiimage = uiimage (data: Datadecoded)! (decodedimageyourimageviewimage = decodedimage        
Objective-cios7 > Version

You can NSData‘s usebase64EncodedStringWithOptions

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 and < version

First Option : Use this link to encode and decode image

ADD Base64 class in your 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 for and encodingdecoding

Reference Links:

1.https://stackoverflow.com/questions/11251340/convert-between-uiimage-and-base64-string

2.https://matrixzk.github.io/blog/20150122/load-image-encoded-with-base64/

Picture 64-bit encoding conversion small note

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.