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 > VersionYou 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 < versionFirst 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 encoding decoding |