ios - imagen - swift image to base64 string
Convierte entre UIImage y Base64 string (14)
En Swift 3.0 y Xcode 8.0
Codificación:
let userImage:UIImage = UIImage(named: "Your-Image_name")!
let imageData:NSData = UIImagePNGRepresentation(userImage)! as NSData
let dataImage = imageData.base64EncodedString(options: .lineLength64Characters)
Decodificación:
let imageData = dataImage
let dataDecode:NSData = NSData(base64Encoded: imageData!, options:.ignoreUnknownCharacters)!
let avatarImage:UIImage = UIImage(data: dataDecode as Data)!
yourImageView.image = avatarImage
¿Alguien sabe cómo convertir un UIImage
a una cadena Base64 y luego invertirlo?
Tengo el siguiente código; la imagen original antes de la codificación es buena, pero solo obtengo una imagen en blanco después de codificarla y decodificarla.
NSData *imageData = UIImagePNGRepresentation(viewImage);
NSString *b64EncStr = [self encode: imageData];
NSString *base64String = [self encodeBase64:imageData];
Rápido
Primero necesitamos tener NSData de la imagen
//Use image name from bundle to create NSData
let image : UIImage = UIImage(named:"imageNameHere")!
//Now use image to create into NSData format
let imageData:NSData = UIImagePNGRepresentation(image)!
//OR next possibility
//Use image''s path to create NSData
let url:NSURL = NSURL(string : "urlHere")!
//Now use image to create into NSData format
let imageData:NSData = NSData.init(contentsOfURL: url)!
Swift 2.0> Codificación
let strBase64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
Swift 2.0> Decodificación
let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)!
Swift 3.0> Decodificación
let dataDecoded : Data = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)!
Codificación :
let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
print(strBase64)
Decodificación :
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
C objetivo
iOS7> versión
Puede usar NSData''s
base64EncodedStringWithOptions
Codificación :
- (NSString *)encodeToBase64String:(UIImage *)image {
return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
Decodificación :
- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
return [UIImage imageWithData:data];
}
iOS 6.1 y <versión
Primera opción : utilice this enlace para encode
y decode
imagen
Agrega la clase Base64 en tu project
.
Codificación :
NSData* data = UIImageJPEGRepresentation(yourImage, 1.0f);
NSString *strEncoded = [Base64 encode:data];
Decodificación :
NSData* data = [Base64 decode:strEncoded ];;
image.image = [UIImage imageWithData:data];
Otra opción : usar QSUtilities para encoding
y decoding
En swift 2.0 usa esta extensión (crédito a Jonas Franz)
extension UIImage{
func toBase64() -> String{
let imageData = UIImagePNGRepresentation(self)!
return imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
}
}
Para iOS 7+, Objective-C, a continuación se explica cómo hacer que la conversión comience con una URL de imagen:
NSURL *url = [NSURL URLWithString:self.groove.thumbnailURL];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
NSString *base64String = [UIImagePNGRepresentation(image)
base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
Swift 3.0 y Xcode 8.0
let imageData = UIImageJPEGRepresentation(imageView.image!, 1)
let base64String = (imageData! as Data).base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
print(base64String)
Swift iOS8
// prgm mark ----
// convert images into base64 and keep them into string
func convertImageToBase64(image: UIImage) -> String {
var imageData = UIImagePNGRepresentation(image)
let base64String = imageData.base64EncodedStringWithOptions(.allZeros)
return base64String
}// end convertImageToBase64
// prgm mark ----
// convert images into base64 and keep them into string
func convertBase64ToImage(base64String: String) -> UIImage {
let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions(rawValue: 0) )
var decodedimage = UIImage(data: decodedData!)
return decodedimage!
}// end convertBase64ToImage
Swift-Extensión:
extension UIImage{
func toBase64() -> String{
var imageData = UIImagePNGRepresentation(self)
return imageData.base64EncodedStringWithOptions(.allZeros)
}
}
En Swift 3.0
func decodeBase64(toImage strEncodeData: String) -> UIImage {
let dataDecoded = NSData(base64Encoded: strEncodeData, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)!
let image = UIImage(data: dataDecoded as Data)
return image!
}
SWIFT 3.0, XCODE 8.0
Reemplace la cadena con su URL. y testImage es una salida de ImageView
// Put Your Image URL
let url:NSURL = NSURL(string : "http://.jpg")!
// It Will turn Into Data
let imageData : NSData = NSData.init(contentsOf: url as URL)!
// Data Will Encode into Base64
let str64 = imageData.base64EncodedData(options: .lineLength64Characters)
// Now Base64 will Decode Here
let data: NSData = NSData(base64Encoded: str64 , options: .ignoreUnknownCharacters)!
// turn Decoded String into Data
let dataImage = UIImage(data: data as Data)
// pass the data image to image View.:)
testImage.image = dataImage
Espero ayuda Gracias.
Swift 3.0
Para convertir la imagen a la cadena base64
Probado en el patio de recreo
var logo = UIImage(named: "image_logo")
let imageData:Data = UIImagePNGRepresentation(logo)
let base64String = imageData.base64EncodedString()
print(base64String)
Versión de Swift: crea base64 para la imagen
En mi opinión, Implicitly Unwrapped Optional en el caso de UIImagePNGRepresenatation()
no es seguro, por lo que recomiendo usar la extensión como a continuación:
extension UIImage {
func toBase64() -> String? {
let imageData = UIImagePNGRepresentation(self)
return imageData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
}
}
XCode 9.1 y Swift 4.0
//
// Convert String to base64
//
class func convertImageToBase64(image: UIImage) -> String {
let imageData = UIImagePNGRepresentation(image)!
return imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters)
}
//
// Convert base64 to String
//
class func convertBase64ToImage(imageString: String) -> UIImage {
let imageData = Data(base64Encoded: imageString, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)!
return UIImage(data: imageData)!
}
@implementation UIImage (Extended)
- (NSString *)base64String {
NSData * data = [UIImagePNGRepresentation(self) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
return [NSString stringWithUTF8String:[data bytes]];
}
@end
See my class - AppExtension.swift
// MARK: - UIImage (Base64 Encoding)
public enum ImageFormat {
case PNG
case JPEG(CGFloat)
}
extension UIImage {
public func base64(format: ImageFormat) -> String {
var imageData: NSData
switch format {
case .PNG: imageData = UIImagePNGRepresentation(self)
case .JPEG(let compression): imageData = UIImageJPEGRepresentation(self, compression)
}
return imageData.base64EncodedStringWithOptions(.allZeros)
}
}