Convert between UIImage and Base64 string

后端 未结 24 2566
抹茶落季
抹茶落季 2020-11-22 01:45

Does anyone know how to convert a UIImage to a Base64 string, and then reverse it?

I have the below code; the original image before encoding is good, bu

相关标签:
24条回答
  • 2020-11-22 02:07

    Swift

    First we need to have image's NSData

    //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 > 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 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 encoding and decoding


    0 讨论(0)
  • 2020-11-22 02:07

    Swift 3.0

    To convert image to base64 string

    Tested in playground

        var logo = UIImage(named: "image_logo")
        let imageData:Data =  UIImagePNGRepresentation(logo)
        let base64String = imageData.base64EncodedString()
        print(base64String)
    
    0 讨论(0)
  • 2020-11-22 02:09

    Decoding using convenience initialiser - Swift 5

    extension UIImage {
        convenience init?(base64String: String) {
            guard let data = Data(base64Encoded: base64String) else { return nil }
            self.init(data: data)
        }
    } 
    
    0 讨论(0)
  • 2020-11-22 02:13

    In 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!
    
    }
    
    0 讨论(0)
  • 2020-11-22 02:13

    Swift 5, Xcode 10.

     let imageData = UIImage(named:"imagename").pngData()?.base64EncodedString(options: .lineLength64Characters)
    
    print(imageData)

    0 讨论(0)
  • 2020-11-22 02:14
    @implementation UIImage (Extended)
    
    - (NSString *)base64String {
        NSData * data = [UIImagePNGRepresentation(self) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
        return [NSString stringWithUTF8String:[data bytes]];
    }
    
    @end
    
    0 讨论(0)
提交回复
热议问题