猿问

UIImage和base 64字符串之间的转换

UIImage和base 64字符串之间的转换

是否有人知道如何将UIImage到一个基本64字符串,然后反转它?

我有下面的代码;编码前的原始图像很好,但只有在编码和解码之后才能得到空白的图像。

NSData *imageData = UIImagePNGRepresentation(viewImage);NSString *b64EncStr = [self encode: imageData];
NSString *base64String = [self encodeBase64:imageData];


九州编程
浏览 913回答 3
3回答

缥缈止盈

首先,我们需要图像的NSData//Use&nbsp;image&nbsp;name&nbsp;from&nbsp;bundle&nbsp;to&nbsp;create&nbsp;NSDatalet&nbsp;image&nbsp;:&nbsp;UIImage&nbsp;=&nbsp;UIImage(named:"imageNameHere")!//Now&nbsp;use&nbsp;image&nbsp;to&nbsp;create&nbsp;into&nbsp;NSData&nbsp; formatlet&nbsp;imageData:NSData&nbsp;=&nbsp;UIImagePNGRepresentation(image)!//OR&nbsp;next&nbsp;possibility//Use&nbsp;image's&nbsp;path&nbsp;to&nbsp;create&nbsp;NSDatalet&nbsp;url:NSURL&nbsp;=&nbsp; NSURL(string&nbsp;:&nbsp;"urlHere")!//Now&nbsp;use&nbsp;image&nbsp;to&nbsp;create&nbsp;into&nbsp;NSData&nbsp;formatlet&nbsp;imageData:NSData&nbsp;=&nbsp;NSData.init(contentsOfURL:&nbsp;url)!SWIFT 2.0>编码let&nbsp;strBase64:String&nbsp;=&nbsp;imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)SWIFT 2.0>解码let&nbsp;dataDecoded:NSData&nbsp;=&nbsp;NSData(base64EncodedString:&nbsp;strBase64,&nbsp;options:&nbsp;NSDataBase64DecodingOptions.IgnoreUnknownCharacters)!SWIFT 3.0>解码let&nbsp;dataDecoded&nbsp;:&nbsp;Data&nbsp;=&nbsp;Data(base64Encoded:&nbsp;strBase64,&nbsp;options:&nbsp;.ignoreUnknownCharacters)!编码&nbsp;:let&nbsp;strBase64&nbsp;=&nbsp;imageData.base64EncodedString(options:&nbsp;.lineLength64Characters)print(strBase64)解码&nbsp;:let&nbsp;dataDecoded:NSData&nbsp;=&nbsp;NSData(base64EncodedString:&nbsp;strBase64,&nbsp;options:&nbsp;NSDataBase64DecodingOptions(rawValue:&nbsp;0))!let&nbsp;decodedimage:UIImage &nbsp;=&nbsp;UIImage(data:&nbsp;dataDecoded)!print(decodedimage)yourImageView.image&nbsp;=&nbsp;decodedimageSWIFT 3.0let&nbsp;dataDecoded&nbsp;:&nbsp;Data&nbsp;=&nbsp;Data(base64Encoded:&nbsp;strBase64,&nbsp;options:&nbsp;.ignoreUnknownCharacters)!let&nbsp;decodedimage&nbsp;=&nbsp;UIImage(data:&nbsp;dataDecoded) yourImageView.image&nbsp;=&nbsp;decodedimage目标-CiOS 7>版本你可以用NSData's&nbsp;base64EncodedStringWithOptions编码&nbsp;:-&nbsp;(NSString&nbsp;*)encodeToBase64String:(UIImage&nbsp;*)image&nbsp;{ &nbsp;return&nbsp;[UIImagePNGRepresentation(image)&nbsp;base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];}解码&nbsp;:-&nbsp;(UIImage&nbsp;*)decodeBase64ToImage:(NSString&nbsp;*)strEncodeData&nbsp;{ &nbsp;&nbsp;NSData&nbsp;*data&nbsp;=&nbsp;[[NSData&nbsp;alloc]initWithBase64EncodedString:strEncodeData&nbsp;options:NSDataBase64DecodingIgnoreUnknownCharacters]; &nbsp;&nbsp;return&nbsp;[UIImage&nbsp;imageWithData:data];}IOS 6.1和<版本第一选择*使用这,这个链接到encode和decode影象加基准64在您的project.编码&nbsp;:&nbsp;NSData*&nbsp;data&nbsp;=&nbsp;UIImageJPEGRepresentation(yourImage,&nbsp;1.0f); &nbsp;NSString&nbsp;*strEncoded&nbsp;=&nbsp;[Base64&nbsp;encode:data];解码&nbsp;:&nbsp;NSData*&nbsp;data&nbsp;=&nbsp;[Base64&nbsp;decode:strEncoded&nbsp;];; &nbsp;image.image&nbsp;=&nbsp;[UIImage&nbsp;imageWithData:data];另一种选择*使用Q实用程序为encoding和decoding

弑天下

SWIFT 4.2扩展方法extension&nbsp;UIImage&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;func&nbsp;toBase64()&nbsp;->&nbsp;String?&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;guard&nbsp;let&nbsp;imageData&nbsp;=&nbsp;self.pngData()&nbsp;else&nbsp;{&nbsp;return&nbsp;nil&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;imageData.base64EncodedString(options:&nbsp;Data.Base64EncodingOptions.lineLength64Characters) &nbsp;&nbsp;&nbsp;&nbsp;}}Xcode 9.1和SWIFT 4.0// //&nbsp;Convert&nbsp;UIImage&nbsp;to&nbsp;a&nbsp;base64&nbsp;representation //class&nbsp;func&nbsp;convertImageToBase64(image:&nbsp;UIImage)&nbsp;->&nbsp;String&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;imageData&nbsp;=&nbsp;UIImagePNGRepresentation(image)! &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;imageData.base64EncodedString(options:&nbsp;Data.Base64EncodingOptions.lineLength64Characters)}// //&nbsp;Convert&nbsp;a&nbsp;base64&nbsp;representation&nbsp;to&nbsp;a&nbsp;UIImage //class&nbsp;func&nbsp;convertBase64ToImage(imageString:&nbsp;String)&nbsp;->&nbsp;UIImage&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;imageData&nbsp;=&nbsp;Data(base64Encoded:&nbsp;imageString,&nbsp;options:&nbsp;Data.Base64DecodingOptions.ignoreUnknownCharacters)! &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;UIImage(data:&nbsp;imageData)!}
随时随地看视频慕课网APP

相关分类

iOS
我要回答