Convert between UIImage and Base64 string












181















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, but I only get a blank image after I encode and decode it.



NSData *imageData = UIImagePNGRepresentation(viewImage);

NSString *b64EncStr = [self encode: imageData];

NSString *base64String = [self encodeBase64:imageData];









share|improve this question

























  • Try this: github.com/l4u/NSData-Base64

    – user529758
    Jun 28 '12 at 19:15











  • Can anyone answer to this question: stackoverflow.com/questions/39657434/…

    – user3011809
    Sep 23 '16 at 12:05
















181















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, but I only get a blank image after I encode and decode it.



NSData *imageData = UIImagePNGRepresentation(viewImage);

NSString *b64EncStr = [self encode: imageData];

NSString *base64String = [self encodeBase64:imageData];









share|improve this question

























  • Try this: github.com/l4u/NSData-Base64

    – user529758
    Jun 28 '12 at 19:15











  • Can anyone answer to this question: stackoverflow.com/questions/39657434/…

    – user3011809
    Sep 23 '16 at 12:05














181












181








181


108






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, but I only get a blank image after I encode and decode it.



NSData *imageData = UIImagePNGRepresentation(viewImage);

NSString *b64EncStr = [self encode: imageData];

NSString *base64String = [self encodeBase64:imageData];









share|improve this question
















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, but I only get a blank image after I encode and decode it.



NSData *imageData = UIImagePNGRepresentation(viewImage);

NSString *b64EncStr = [self encode: imageData];

NSString *base64String = [self encodeBase64:imageData];






ios swift uiimage base64 nsdata






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 5 '18 at 13:06









Ashish Kakkad

18.4k869109




18.4k869109










asked Jun 28 '12 at 19:11









changeychangey

7,00671827




7,00671827













  • Try this: github.com/l4u/NSData-Base64

    – user529758
    Jun 28 '12 at 19:15











  • Can anyone answer to this question: stackoverflow.com/questions/39657434/…

    – user3011809
    Sep 23 '16 at 12:05



















  • Try this: github.com/l4u/NSData-Base64

    – user529758
    Jun 28 '12 at 19:15











  • Can anyone answer to this question: stackoverflow.com/questions/39657434/…

    – user3011809
    Sep 23 '16 at 12:05

















Try this: github.com/l4u/NSData-Base64

– user529758
Jun 28 '12 at 19:15





Try this: github.com/l4u/NSData-Base64

– user529758
Jun 28 '12 at 19:15













Can anyone answer to this question: stackoverflow.com/questions/39657434/…

– user3011809
Sep 23 '16 at 12:05





Can anyone answer to this question: stackoverflow.com/questions/39657434/…

– user3011809
Sep 23 '16 at 12:05












19 Answers
19






active

oldest

votes


















523














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









share|improve this answer





















  • 7





    -1 vote down for what

    – Paresh Navadiya
    Jun 28 '12 at 19:37






  • 1





    @Safecase I wasn't the person that voted you down, but possibly the double post is the offence?

    – Tommy
    Jun 28 '12 at 20:05






  • 2





    Base64 Class sleeps forever, I always force quite xcode if I use this even after making the image quality to 0.001f

    – shebelaw
    Jan 10 '13 at 0:15






  • 5





    Important note: make sure your base64 string doesn't include the prefix required on a browser to display, e.g. data:image/jpeg;base64,

    – Tyler Sheaffer
    Jan 10 '17 at 22:15






  • 2





    @TylerSheaffer I think this is an important message, which should be included in the answer.

    – Timeless
    Mar 16 '17 at 7:48



















19














SWIFT 3.0, XCODE 8.0



Replace String with your URL. and testImage is an outlet of 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


Hope It Helps Thanks.






share|improve this answer

































    18














    Swift 4.2 Extension method



    extension UIImage {
    func toBase64() -> String? {
    guard let imageData = self.pngData() else { return nil }
    return imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters)
    }
    }


    XCode 9.1 and Swift 4.0



    //
    // Convert UIImage to a base64 representation
    //
    class func convertImageToBase64(image: UIImage) -> String {
    let imageData = UIImagePNGRepresentation(image)!
    return imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters)
    }

    //
    // Convert a base64 representation to a UIImage
    //
    class func convertBase64ToImage(imageString: String) -> UIImage {
    let imageData = Data(base64Encoded: imageString, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)!
    return UIImage(data: imageData)!
    }





    share|improve this answer

































      11














      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





      share|improve this answer































        7














        @implementation UIImage (Extended)

        - (NSString *)base64String {
        NSData * data = [UIImagePNGRepresentation(self) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
        return [NSString stringWithUTF8String:[data bytes]];
        }

        @end





        share|improve this answer



















        • 1





          Simple and elegant. Love it!

          – Myxtic
          Dec 18 '14 at 19:27



















        7














        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)





        share|improve this answer

































          6














          In swift 2.0 use this extension (credit to Jonas Franz)



          extension UIImage{
          func toBase64() -> String{
          let imageData = UIImagePNGRepresentation(self)!
          return imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
          }
          }





          share|improve this answer































            4














            Swift-Extension:



            extension UIImage{
            func toBase64() -> String{
            var imageData = UIImagePNGRepresentation(self)
            return imageData.base64EncodedStringWithOptions(.allZeros)
            }
            }





            share|improve this answer


























            • For Swift 2.2, .allZeros didn't compile. So I used return imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))

              – Carl Smith
              Jun 7 '16 at 1:24











            • Use Encoding64CharacterLineLength instead of allZero

              – Jonas Franz
              Jun 7 '16 at 4:56






            • 1





              im using switft3, unable to convert to send to server: let imageData = UIImagePNGRepresentation(ImageView.image!); let strBase64:String = imageData!.base64EncodedString()

              – Cmag
              Nov 20 '16 at 4:05













            • @Cmag I know your comment is 2 years old but did you manage to fix it?

              – ZUNJAE
              Nov 27 '18 at 7:57











            • @ZUNJAE am afraid i dont recall how i fixed it, dont have access to the solution any more

              – Cmag
              Dec 3 '18 at 5:53



















            4














            In Swift 3.0 and Xcode 8.0



            Encoding :



            let userImage:UIImage = UIImage(named: "Your-Image_name")!
            let imageData:NSData = UIImagePNGRepresentation(userImage)! as NSData
            let dataImage = imageData.base64EncodedString(options: .lineLength64Characters)


            Decoding :



            let imageData = dataImage
            let dataDecode:NSData = NSData(base64Encoded: imageData!, options:.ignoreUnknownCharacters)!
            let avatarImage:UIImage = UIImage(data: dataDecode as Data)!
            yourImageView.image = avatarImage





            share|improve this answer
























            • Great thank you

              – Mc.Lover
              Dec 19 '16 at 8:05











            • i can't thank you enough

              – ggnoredo
              Feb 6 '18 at 11:41



















            4














            Swift 4



            Encoding



            func ConvertImageToBase64String (img: UIImage) -> String {
            let imageData:NSData = UIImageJPEGRepresentation(img, 0.50)! as NSData //UIImagePNGRepresentation(img)
            let imgString = imageData.base64EncodedString(options: .init(rawValue: 0))
            return imgString
            }


            Decoding



            func ConvertBase64StringToImage (imageBase64String:String) -> UIImage {
            let imageData = Data.init(base64Encoded: imageBase64String, options: .init(rawValue: 0))
            let image = UIImage(data: imageData!)
            return image
            }


            Note: Tested in xcode 9.4.1






            share|improve this answer
























            • now wrong swift 4

              – Masum Biswas
              Sep 27 '18 at 11:16











            • Thank you for comment on my answer, Can you please up vote my answer, this is very helpful for me.

              – Vivek
              Sep 27 '18 at 14:17



















            1














            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)
            }
            }





            share|improve this answer































              1














              Swift version - create base64 for image



              In my opinion Implicitly Unwrapped Optional in case of UIImagePNGRepresenatation() is not safe, so I recommend to use extension like below:



              extension UIImage {

              func toBase64() -> String? {
              let imageData = UIImagePNGRepresentation(self)
              return imageData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
              }
              }





              share|improve this answer































                1














                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!

                }





                share|improve this answer































                  1














                  Swift 4.2 | Xcode 10



                  extension UIImage {

                  /// EZSE: Returns base64 string
                  public var base64: String {
                  return self.jpegData(compressionQuality: 1.0)!.base64EncodedString()
                  }
                  }





                  share|improve this answer































                    1














                    Swift 4



                    enum ImageFormat {
                    case png
                    case jpeg(CGFloat)
                    }

                    extension UIImage {
                    func base64(format: ImageFormat) -> String? {
                    var imageData: Data?

                    switch format {
                    case .png: imageData = UIImagePNGRepresentation(self)
                    case .jpeg(let compression): imageData = UIImageJPEGRepresentation(self, compression)
                    }

                    return imageData?.base64EncodedString()
                    }
                    }

                    extension String {
                    func imageFromBase64() -> UIImage? {
                    guard let data = Data(base64Encoded: self) else { return nil }

                    return UIImage(data: data)
                    }
                    }





                    share|improve this answer

































                      0














                      For iOS 7+, Objective-C, here's how to make the conversion starting with an image URL:



                      NSURL *url = [NSURL URLWithString:self.groove.thumbnailURL];

                      UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

                      NSString *base64String = [UIImagePNGRepresentation(image)
                      base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];





                      share|improve this answer

































                        0














                        Swift 3.0 and Xcode 8.0



                        let imageData = UIImageJPEGRepresentation(imageView.image!, 1)

                        let base64String = (imageData! as Data).base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
                        print(base64String)





                        share|improve this answer































                          0














                          I tried all the solutions, none worked for me (using Swift 4), this is the solution that worked for me, if anyone in future faces the same problem.



                          let temp = base64String.components(separatedBy: ",")
                          let dataDecoded : Data = Data(base64Encoded: temp[1], options:
                          .ignoreUnknownCharacters)!
                          let decodedimage = UIImage(data: dataDecoded)

                          yourImage.image = decodedimage





                          share|improve this answer































                            0














                            Swift 4.2, Xcode 10.1



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



                            print(imageData)






                            share|improve this answer























                              Your Answer






                              StackExchange.ifUsing("editor", function () {
                              StackExchange.using("externalEditor", function () {
                              StackExchange.using("snippets", function () {
                              StackExchange.snippets.init();
                              });
                              });
                              }, "code-snippets");

                              StackExchange.ready(function() {
                              var channelOptions = {
                              tags: "".split(" "),
                              id: "1"
                              };
                              initTagRenderer("".split(" "), "".split(" "), channelOptions);

                              StackExchange.using("externalEditor", function() {
                              // Have to fire editor after snippets, if snippets enabled
                              if (StackExchange.settings.snippets.snippetsEnabled) {
                              StackExchange.using("snippets", function() {
                              createEditor();
                              });
                              }
                              else {
                              createEditor();
                              }
                              });

                              function createEditor() {
                              StackExchange.prepareEditor({
                              heartbeatType: 'answer',
                              autoActivateHeartbeat: false,
                              convertImagesToLinks: true,
                              noModals: true,
                              showLowRepImageUploadWarning: true,
                              reputationToPostImages: 10,
                              bindNavPrevention: true,
                              postfix: "",
                              imageUploader: {
                              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                              allowUrls: true
                              },
                              onDemand: true,
                              discardSelector: ".discard-answer"
                              ,immediatelyShowMarkdownHelp:true
                              });


                              }
                              });














                              draft saved

                              draft discarded


















                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11251340%2fconvert-between-uiimage-and-base64-string%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown

























                              19 Answers
                              19






                              active

                              oldest

                              votes








                              19 Answers
                              19






                              active

                              oldest

                              votes









                              active

                              oldest

                              votes






                              active

                              oldest

                              votes









                              523














                              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









                              share|improve this answer





















                              • 7





                                -1 vote down for what

                                – Paresh Navadiya
                                Jun 28 '12 at 19:37






                              • 1





                                @Safecase I wasn't the person that voted you down, but possibly the double post is the offence?

                                – Tommy
                                Jun 28 '12 at 20:05






                              • 2





                                Base64 Class sleeps forever, I always force quite xcode if I use this even after making the image quality to 0.001f

                                – shebelaw
                                Jan 10 '13 at 0:15






                              • 5





                                Important note: make sure your base64 string doesn't include the prefix required on a browser to display, e.g. data:image/jpeg;base64,

                                – Tyler Sheaffer
                                Jan 10 '17 at 22:15






                              • 2





                                @TylerSheaffer I think this is an important message, which should be included in the answer.

                                – Timeless
                                Mar 16 '17 at 7:48
















                              523














                              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









                              share|improve this answer





















                              • 7





                                -1 vote down for what

                                – Paresh Navadiya
                                Jun 28 '12 at 19:37






                              • 1





                                @Safecase I wasn't the person that voted you down, but possibly the double post is the offence?

                                – Tommy
                                Jun 28 '12 at 20:05






                              • 2





                                Base64 Class sleeps forever, I always force quite xcode if I use this even after making the image quality to 0.001f

                                – shebelaw
                                Jan 10 '13 at 0:15






                              • 5





                                Important note: make sure your base64 string doesn't include the prefix required on a browser to display, e.g. data:image/jpeg;base64,

                                – Tyler Sheaffer
                                Jan 10 '17 at 22:15






                              • 2





                                @TylerSheaffer I think this is an important message, which should be included in the answer.

                                – Timeless
                                Mar 16 '17 at 7:48














                              523












                              523








                              523







                              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









                              share|improve this answer















                              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










                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Jun 12 '17 at 7:04









                              Jacopo Penzo

                              1,44821827




                              1,44821827










                              answered Jun 28 '12 at 19:22









                              Paresh NavadiyaParesh Navadiya

                              34.3k973122




                              34.3k973122








                              • 7





                                -1 vote down for what

                                – Paresh Navadiya
                                Jun 28 '12 at 19:37






                              • 1





                                @Safecase I wasn't the person that voted you down, but possibly the double post is the offence?

                                – Tommy
                                Jun 28 '12 at 20:05






                              • 2





                                Base64 Class sleeps forever, I always force quite xcode if I use this even after making the image quality to 0.001f

                                – shebelaw
                                Jan 10 '13 at 0:15






                              • 5





                                Important note: make sure your base64 string doesn't include the prefix required on a browser to display, e.g. data:image/jpeg;base64,

                                – Tyler Sheaffer
                                Jan 10 '17 at 22:15






                              • 2





                                @TylerSheaffer I think this is an important message, which should be included in the answer.

                                – Timeless
                                Mar 16 '17 at 7:48














                              • 7





                                -1 vote down for what

                                – Paresh Navadiya
                                Jun 28 '12 at 19:37






                              • 1





                                @Safecase I wasn't the person that voted you down, but possibly the double post is the offence?

                                – Tommy
                                Jun 28 '12 at 20:05






                              • 2





                                Base64 Class sleeps forever, I always force quite xcode if I use this even after making the image quality to 0.001f

                                – shebelaw
                                Jan 10 '13 at 0:15






                              • 5





                                Important note: make sure your base64 string doesn't include the prefix required on a browser to display, e.g. data:image/jpeg;base64,

                                – Tyler Sheaffer
                                Jan 10 '17 at 22:15






                              • 2





                                @TylerSheaffer I think this is an important message, which should be included in the answer.

                                – Timeless
                                Mar 16 '17 at 7:48








                              7




                              7





                              -1 vote down for what

                              – Paresh Navadiya
                              Jun 28 '12 at 19:37





                              -1 vote down for what

                              – Paresh Navadiya
                              Jun 28 '12 at 19:37




                              1




                              1





                              @Safecase I wasn't the person that voted you down, but possibly the double post is the offence?

                              – Tommy
                              Jun 28 '12 at 20:05





                              @Safecase I wasn't the person that voted you down, but possibly the double post is the offence?

                              – Tommy
                              Jun 28 '12 at 20:05




                              2




                              2





                              Base64 Class sleeps forever, I always force quite xcode if I use this even after making the image quality to 0.001f

                              – shebelaw
                              Jan 10 '13 at 0:15





                              Base64 Class sleeps forever, I always force quite xcode if I use this even after making the image quality to 0.001f

                              – shebelaw
                              Jan 10 '13 at 0:15




                              5




                              5





                              Important note: make sure your base64 string doesn't include the prefix required on a browser to display, e.g. data:image/jpeg;base64,

                              – Tyler Sheaffer
                              Jan 10 '17 at 22:15





                              Important note: make sure your base64 string doesn't include the prefix required on a browser to display, e.g. data:image/jpeg;base64,

                              – Tyler Sheaffer
                              Jan 10 '17 at 22:15




                              2




                              2





                              @TylerSheaffer I think this is an important message, which should be included in the answer.

                              – Timeless
                              Mar 16 '17 at 7:48





                              @TylerSheaffer I think this is an important message, which should be included in the answer.

                              – Timeless
                              Mar 16 '17 at 7:48













                              19














                              SWIFT 3.0, XCODE 8.0



                              Replace String with your URL. and testImage is an outlet of 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


                              Hope It Helps Thanks.






                              share|improve this answer






























                                19














                                SWIFT 3.0, XCODE 8.0



                                Replace String with your URL. and testImage is an outlet of 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


                                Hope It Helps Thanks.






                                share|improve this answer




























                                  19












                                  19








                                  19







                                  SWIFT 3.0, XCODE 8.0



                                  Replace String with your URL. and testImage is an outlet of 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


                                  Hope It Helps Thanks.






                                  share|improve this answer















                                  SWIFT 3.0, XCODE 8.0



                                  Replace String with your URL. and testImage is an outlet of 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


                                  Hope It Helps Thanks.







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Apr 5 '17 at 10:32









                                  Naresh Reddy M

                                  7591726




                                  7591726










                                  answered Oct 19 '16 at 6:16









                                  Avinash MishraAvinash Mishra

                                  20125




                                  20125























                                      18














                                      Swift 4.2 Extension method



                                      extension UIImage {
                                      func toBase64() -> String? {
                                      guard let imageData = self.pngData() else { return nil }
                                      return imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters)
                                      }
                                      }


                                      XCode 9.1 and Swift 4.0



                                      //
                                      // Convert UIImage to a base64 representation
                                      //
                                      class func convertImageToBase64(image: UIImage) -> String {
                                      let imageData = UIImagePNGRepresentation(image)!
                                      return imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters)
                                      }

                                      //
                                      // Convert a base64 representation to a UIImage
                                      //
                                      class func convertBase64ToImage(imageString: String) -> UIImage {
                                      let imageData = Data(base64Encoded: imageString, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)!
                                      return UIImage(data: imageData)!
                                      }





                                      share|improve this answer






























                                        18














                                        Swift 4.2 Extension method



                                        extension UIImage {
                                        func toBase64() -> String? {
                                        guard let imageData = self.pngData() else { return nil }
                                        return imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters)
                                        }
                                        }


                                        XCode 9.1 and Swift 4.0



                                        //
                                        // Convert UIImage to a base64 representation
                                        //
                                        class func convertImageToBase64(image: UIImage) -> String {
                                        let imageData = UIImagePNGRepresentation(image)!
                                        return imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters)
                                        }

                                        //
                                        // Convert a base64 representation to a UIImage
                                        //
                                        class func convertBase64ToImage(imageString: String) -> UIImage {
                                        let imageData = Data(base64Encoded: imageString, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)!
                                        return UIImage(data: imageData)!
                                        }





                                        share|improve this answer




























                                          18












                                          18








                                          18







                                          Swift 4.2 Extension method



                                          extension UIImage {
                                          func toBase64() -> String? {
                                          guard let imageData = self.pngData() else { return nil }
                                          return imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters)
                                          }
                                          }


                                          XCode 9.1 and Swift 4.0



                                          //
                                          // Convert UIImage to a base64 representation
                                          //
                                          class func convertImageToBase64(image: UIImage) -> String {
                                          let imageData = UIImagePNGRepresentation(image)!
                                          return imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters)
                                          }

                                          //
                                          // Convert a base64 representation to a UIImage
                                          //
                                          class func convertBase64ToImage(imageString: String) -> UIImage {
                                          let imageData = Data(base64Encoded: imageString, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)!
                                          return UIImage(data: imageData)!
                                          }





                                          share|improve this answer















                                          Swift 4.2 Extension method



                                          extension UIImage {
                                          func toBase64() -> String? {
                                          guard let imageData = self.pngData() else { return nil }
                                          return imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters)
                                          }
                                          }


                                          XCode 9.1 and Swift 4.0



                                          //
                                          // Convert UIImage to a base64 representation
                                          //
                                          class func convertImageToBase64(image: UIImage) -> String {
                                          let imageData = UIImagePNGRepresentation(image)!
                                          return imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters)
                                          }

                                          //
                                          // Convert a base64 representation to a UIImage
                                          //
                                          class func convertBase64ToImage(imageString: String) -> UIImage {
                                          let imageData = Data(base64Encoded: imageString, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)!
                                          return UIImage(data: imageData)!
                                          }






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Nov 22 '18 at 14:59









                                          ZUNJAE

                                          6922718




                                          6922718










                                          answered Dec 2 '17 at 17:48









                                          Souf RSouf R

                                          382416




                                          382416























                                              11














                                              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





                                              share|improve this answer




























                                                11














                                                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





                                                share|improve this answer


























                                                  11












                                                  11








                                                  11







                                                  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





                                                  share|improve this answer













                                                  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






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Feb 10 '15 at 11:01









                                                  Vinod JoshiVinod Joshi

                                                  5,8223845




                                                  5,8223845























                                                      7














                                                      @implementation UIImage (Extended)

                                                      - (NSString *)base64String {
                                                      NSData * data = [UIImagePNGRepresentation(self) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
                                                      return [NSString stringWithUTF8String:[data bytes]];
                                                      }

                                                      @end





                                                      share|improve this answer



















                                                      • 1





                                                        Simple and elegant. Love it!

                                                        – Myxtic
                                                        Dec 18 '14 at 19:27
















                                                      7














                                                      @implementation UIImage (Extended)

                                                      - (NSString *)base64String {
                                                      NSData * data = [UIImagePNGRepresentation(self) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
                                                      return [NSString stringWithUTF8String:[data bytes]];
                                                      }

                                                      @end





                                                      share|improve this answer



















                                                      • 1





                                                        Simple and elegant. Love it!

                                                        – Myxtic
                                                        Dec 18 '14 at 19:27














                                                      7












                                                      7








                                                      7







                                                      @implementation UIImage (Extended)

                                                      - (NSString *)base64String {
                                                      NSData * data = [UIImagePNGRepresentation(self) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
                                                      return [NSString stringWithUTF8String:[data bytes]];
                                                      }

                                                      @end





                                                      share|improve this answer













                                                      @implementation UIImage (Extended)

                                                      - (NSString *)base64String {
                                                      NSData * data = [UIImagePNGRepresentation(self) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
                                                      return [NSString stringWithUTF8String:[data bytes]];
                                                      }

                                                      @end






                                                      share|improve this answer












                                                      share|improve this answer



                                                      share|improve this answer










                                                      answered Oct 2 '13 at 15:17









                                                      Peter LapisuPeter Lapisu

                                                      12.3k1192146




                                                      12.3k1192146








                                                      • 1





                                                        Simple and elegant. Love it!

                                                        – Myxtic
                                                        Dec 18 '14 at 19:27














                                                      • 1





                                                        Simple and elegant. Love it!

                                                        – Myxtic
                                                        Dec 18 '14 at 19:27








                                                      1




                                                      1





                                                      Simple and elegant. Love it!

                                                      – Myxtic
                                                      Dec 18 '14 at 19:27





                                                      Simple and elegant. Love it!

                                                      – Myxtic
                                                      Dec 18 '14 at 19:27











                                                      7














                                                      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)





                                                      share|improve this answer






























                                                        7














                                                        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)





                                                        share|improve this answer




























                                                          7












                                                          7








                                                          7







                                                          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)





                                                          share|improve this answer















                                                          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)






                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited Jan 22 '17 at 14:23

























                                                          answered Jan 22 '17 at 14:14









                                                          dimo hamdydimo hamdy

                                                          1,1711311




                                                          1,1711311























                                                              6














                                                              In swift 2.0 use this extension (credit to Jonas Franz)



                                                              extension UIImage{
                                                              func toBase64() -> String{
                                                              let imageData = UIImagePNGRepresentation(self)!
                                                              return imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
                                                              }
                                                              }





                                                              share|improve this answer




























                                                                6














                                                                In swift 2.0 use this extension (credit to Jonas Franz)



                                                                extension UIImage{
                                                                func toBase64() -> String{
                                                                let imageData = UIImagePNGRepresentation(self)!
                                                                return imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
                                                                }
                                                                }





                                                                share|improve this answer


























                                                                  6












                                                                  6








                                                                  6







                                                                  In swift 2.0 use this extension (credit to Jonas Franz)



                                                                  extension UIImage{
                                                                  func toBase64() -> String{
                                                                  let imageData = UIImagePNGRepresentation(self)!
                                                                  return imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
                                                                  }
                                                                  }





                                                                  share|improve this answer













                                                                  In swift 2.0 use this extension (credit to Jonas Franz)



                                                                  extension UIImage{
                                                                  func toBase64() -> String{
                                                                  let imageData = UIImagePNGRepresentation(self)!
                                                                  return imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
                                                                  }
                                                                  }






                                                                  share|improve this answer












                                                                  share|improve this answer



                                                                  share|improve this answer










                                                                  answered Oct 23 '15 at 17:33









                                                                  Ciprian RarauCiprian Rarau

                                                                  1,6811622




                                                                  1,6811622























                                                                      4














                                                                      Swift-Extension:



                                                                      extension UIImage{
                                                                      func toBase64() -> String{
                                                                      var imageData = UIImagePNGRepresentation(self)
                                                                      return imageData.base64EncodedStringWithOptions(.allZeros)
                                                                      }
                                                                      }





                                                                      share|improve this answer


























                                                                      • For Swift 2.2, .allZeros didn't compile. So I used return imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))

                                                                        – Carl Smith
                                                                        Jun 7 '16 at 1:24











                                                                      • Use Encoding64CharacterLineLength instead of allZero

                                                                        – Jonas Franz
                                                                        Jun 7 '16 at 4:56






                                                                      • 1





                                                                        im using switft3, unable to convert to send to server: let imageData = UIImagePNGRepresentation(ImageView.image!); let strBase64:String = imageData!.base64EncodedString()

                                                                        – Cmag
                                                                        Nov 20 '16 at 4:05













                                                                      • @Cmag I know your comment is 2 years old but did you manage to fix it?

                                                                        – ZUNJAE
                                                                        Nov 27 '18 at 7:57











                                                                      • @ZUNJAE am afraid i dont recall how i fixed it, dont have access to the solution any more

                                                                        – Cmag
                                                                        Dec 3 '18 at 5:53
















                                                                      4














                                                                      Swift-Extension:



                                                                      extension UIImage{
                                                                      func toBase64() -> String{
                                                                      var imageData = UIImagePNGRepresentation(self)
                                                                      return imageData.base64EncodedStringWithOptions(.allZeros)
                                                                      }
                                                                      }





                                                                      share|improve this answer


























                                                                      • For Swift 2.2, .allZeros didn't compile. So I used return imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))

                                                                        – Carl Smith
                                                                        Jun 7 '16 at 1:24











                                                                      • Use Encoding64CharacterLineLength instead of allZero

                                                                        – Jonas Franz
                                                                        Jun 7 '16 at 4:56






                                                                      • 1





                                                                        im using switft3, unable to convert to send to server: let imageData = UIImagePNGRepresentation(ImageView.image!); let strBase64:String = imageData!.base64EncodedString()

                                                                        – Cmag
                                                                        Nov 20 '16 at 4:05













                                                                      • @Cmag I know your comment is 2 years old but did you manage to fix it?

                                                                        – ZUNJAE
                                                                        Nov 27 '18 at 7:57











                                                                      • @ZUNJAE am afraid i dont recall how i fixed it, dont have access to the solution any more

                                                                        – Cmag
                                                                        Dec 3 '18 at 5:53














                                                                      4












                                                                      4








                                                                      4







                                                                      Swift-Extension:



                                                                      extension UIImage{
                                                                      func toBase64() -> String{
                                                                      var imageData = UIImagePNGRepresentation(self)
                                                                      return imageData.base64EncodedStringWithOptions(.allZeros)
                                                                      }
                                                                      }





                                                                      share|improve this answer















                                                                      Swift-Extension:



                                                                      extension UIImage{
                                                                      func toBase64() -> String{
                                                                      var imageData = UIImagePNGRepresentation(self)
                                                                      return imageData.base64EncodedStringWithOptions(.allZeros)
                                                                      }
                                                                      }






                                                                      share|improve this answer














                                                                      share|improve this answer



                                                                      share|improve this answer








                                                                      edited Sep 8 '15 at 8:45









                                                                      EI Captain v2.0

                                                                      18.4k96184




                                                                      18.4k96184










                                                                      answered Jul 2 '15 at 15:38









                                                                      Jonas FranzJonas Franz

                                                                      3101515




                                                                      3101515













                                                                      • For Swift 2.2, .allZeros didn't compile. So I used return imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))

                                                                        – Carl Smith
                                                                        Jun 7 '16 at 1:24











                                                                      • Use Encoding64CharacterLineLength instead of allZero

                                                                        – Jonas Franz
                                                                        Jun 7 '16 at 4:56






                                                                      • 1





                                                                        im using switft3, unable to convert to send to server: let imageData = UIImagePNGRepresentation(ImageView.image!); let strBase64:String = imageData!.base64EncodedString()

                                                                        – Cmag
                                                                        Nov 20 '16 at 4:05













                                                                      • @Cmag I know your comment is 2 years old but did you manage to fix it?

                                                                        – ZUNJAE
                                                                        Nov 27 '18 at 7:57











                                                                      • @ZUNJAE am afraid i dont recall how i fixed it, dont have access to the solution any more

                                                                        – Cmag
                                                                        Dec 3 '18 at 5:53



















                                                                      • For Swift 2.2, .allZeros didn't compile. So I used return imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))

                                                                        – Carl Smith
                                                                        Jun 7 '16 at 1:24











                                                                      • Use Encoding64CharacterLineLength instead of allZero

                                                                        – Jonas Franz
                                                                        Jun 7 '16 at 4:56






                                                                      • 1





                                                                        im using switft3, unable to convert to send to server: let imageData = UIImagePNGRepresentation(ImageView.image!); let strBase64:String = imageData!.base64EncodedString()

                                                                        – Cmag
                                                                        Nov 20 '16 at 4:05













                                                                      • @Cmag I know your comment is 2 years old but did you manage to fix it?

                                                                        – ZUNJAE
                                                                        Nov 27 '18 at 7:57











                                                                      • @ZUNJAE am afraid i dont recall how i fixed it, dont have access to the solution any more

                                                                        – Cmag
                                                                        Dec 3 '18 at 5:53

















                                                                      For Swift 2.2, .allZeros didn't compile. So I used return imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))

                                                                      – Carl Smith
                                                                      Jun 7 '16 at 1:24





                                                                      For Swift 2.2, .allZeros didn't compile. So I used return imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))

                                                                      – Carl Smith
                                                                      Jun 7 '16 at 1:24













                                                                      Use Encoding64CharacterLineLength instead of allZero

                                                                      – Jonas Franz
                                                                      Jun 7 '16 at 4:56





                                                                      Use Encoding64CharacterLineLength instead of allZero

                                                                      – Jonas Franz
                                                                      Jun 7 '16 at 4:56




                                                                      1




                                                                      1





                                                                      im using switft3, unable to convert to send to server: let imageData = UIImagePNGRepresentation(ImageView.image!); let strBase64:String = imageData!.base64EncodedString()

                                                                      – Cmag
                                                                      Nov 20 '16 at 4:05







                                                                      im using switft3, unable to convert to send to server: let imageData = UIImagePNGRepresentation(ImageView.image!); let strBase64:String = imageData!.base64EncodedString()

                                                                      – Cmag
                                                                      Nov 20 '16 at 4:05















                                                                      @Cmag I know your comment is 2 years old but did you manage to fix it?

                                                                      – ZUNJAE
                                                                      Nov 27 '18 at 7:57





                                                                      @Cmag I know your comment is 2 years old but did you manage to fix it?

                                                                      – ZUNJAE
                                                                      Nov 27 '18 at 7:57













                                                                      @ZUNJAE am afraid i dont recall how i fixed it, dont have access to the solution any more

                                                                      – Cmag
                                                                      Dec 3 '18 at 5:53





                                                                      @ZUNJAE am afraid i dont recall how i fixed it, dont have access to the solution any more

                                                                      – Cmag
                                                                      Dec 3 '18 at 5:53











                                                                      4














                                                                      In Swift 3.0 and Xcode 8.0



                                                                      Encoding :



                                                                      let userImage:UIImage = UIImage(named: "Your-Image_name")!
                                                                      let imageData:NSData = UIImagePNGRepresentation(userImage)! as NSData
                                                                      let dataImage = imageData.base64EncodedString(options: .lineLength64Characters)


                                                                      Decoding :



                                                                      let imageData = dataImage
                                                                      let dataDecode:NSData = NSData(base64Encoded: imageData!, options:.ignoreUnknownCharacters)!
                                                                      let avatarImage:UIImage = UIImage(data: dataDecode as Data)!
                                                                      yourImageView.image = avatarImage





                                                                      share|improve this answer
























                                                                      • Great thank you

                                                                        – Mc.Lover
                                                                        Dec 19 '16 at 8:05











                                                                      • i can't thank you enough

                                                                        – ggnoredo
                                                                        Feb 6 '18 at 11:41
















                                                                      4














                                                                      In Swift 3.0 and Xcode 8.0



                                                                      Encoding :



                                                                      let userImage:UIImage = UIImage(named: "Your-Image_name")!
                                                                      let imageData:NSData = UIImagePNGRepresentation(userImage)! as NSData
                                                                      let dataImage = imageData.base64EncodedString(options: .lineLength64Characters)


                                                                      Decoding :



                                                                      let imageData = dataImage
                                                                      let dataDecode:NSData = NSData(base64Encoded: imageData!, options:.ignoreUnknownCharacters)!
                                                                      let avatarImage:UIImage = UIImage(data: dataDecode as Data)!
                                                                      yourImageView.image = avatarImage





                                                                      share|improve this answer
























                                                                      • Great thank you

                                                                        – Mc.Lover
                                                                        Dec 19 '16 at 8:05











                                                                      • i can't thank you enough

                                                                        – ggnoredo
                                                                        Feb 6 '18 at 11:41














                                                                      4












                                                                      4








                                                                      4







                                                                      In Swift 3.0 and Xcode 8.0



                                                                      Encoding :



                                                                      let userImage:UIImage = UIImage(named: "Your-Image_name")!
                                                                      let imageData:NSData = UIImagePNGRepresentation(userImage)! as NSData
                                                                      let dataImage = imageData.base64EncodedString(options: .lineLength64Characters)


                                                                      Decoding :



                                                                      let imageData = dataImage
                                                                      let dataDecode:NSData = NSData(base64Encoded: imageData!, options:.ignoreUnknownCharacters)!
                                                                      let avatarImage:UIImage = UIImage(data: dataDecode as Data)!
                                                                      yourImageView.image = avatarImage





                                                                      share|improve this answer













                                                                      In Swift 3.0 and Xcode 8.0



                                                                      Encoding :



                                                                      let userImage:UIImage = UIImage(named: "Your-Image_name")!
                                                                      let imageData:NSData = UIImagePNGRepresentation(userImage)! as NSData
                                                                      let dataImage = imageData.base64EncodedString(options: .lineLength64Characters)


                                                                      Decoding :



                                                                      let imageData = dataImage
                                                                      let dataDecode:NSData = NSData(base64Encoded: imageData!, options:.ignoreUnknownCharacters)!
                                                                      let avatarImage:UIImage = UIImage(data: dataDecode as Data)!
                                                                      yourImageView.image = avatarImage






                                                                      share|improve this answer












                                                                      share|improve this answer



                                                                      share|improve this answer










                                                                      answered Dec 15 '16 at 6:37









                                                                      M.NadeeshanM.Nadeeshan

                                                                      14116




                                                                      14116













                                                                      • Great thank you

                                                                        – Mc.Lover
                                                                        Dec 19 '16 at 8:05











                                                                      • i can't thank you enough

                                                                        – ggnoredo
                                                                        Feb 6 '18 at 11:41



















                                                                      • Great thank you

                                                                        – Mc.Lover
                                                                        Dec 19 '16 at 8:05











                                                                      • i can't thank you enough

                                                                        – ggnoredo
                                                                        Feb 6 '18 at 11:41

















                                                                      Great thank you

                                                                      – Mc.Lover
                                                                      Dec 19 '16 at 8:05





                                                                      Great thank you

                                                                      – Mc.Lover
                                                                      Dec 19 '16 at 8:05













                                                                      i can't thank you enough

                                                                      – ggnoredo
                                                                      Feb 6 '18 at 11:41





                                                                      i can't thank you enough

                                                                      – ggnoredo
                                                                      Feb 6 '18 at 11:41











                                                                      4














                                                                      Swift 4



                                                                      Encoding



                                                                      func ConvertImageToBase64String (img: UIImage) -> String {
                                                                      let imageData:NSData = UIImageJPEGRepresentation(img, 0.50)! as NSData //UIImagePNGRepresentation(img)
                                                                      let imgString = imageData.base64EncodedString(options: .init(rawValue: 0))
                                                                      return imgString
                                                                      }


                                                                      Decoding



                                                                      func ConvertBase64StringToImage (imageBase64String:String) -> UIImage {
                                                                      let imageData = Data.init(base64Encoded: imageBase64String, options: .init(rawValue: 0))
                                                                      let image = UIImage(data: imageData!)
                                                                      return image
                                                                      }


                                                                      Note: Tested in xcode 9.4.1






                                                                      share|improve this answer
























                                                                      • now wrong swift 4

                                                                        – Masum Biswas
                                                                        Sep 27 '18 at 11:16











                                                                      • Thank you for comment on my answer, Can you please up vote my answer, this is very helpful for me.

                                                                        – Vivek
                                                                        Sep 27 '18 at 14:17
















                                                                      4














                                                                      Swift 4



                                                                      Encoding



                                                                      func ConvertImageToBase64String (img: UIImage) -> String {
                                                                      let imageData:NSData = UIImageJPEGRepresentation(img, 0.50)! as NSData //UIImagePNGRepresentation(img)
                                                                      let imgString = imageData.base64EncodedString(options: .init(rawValue: 0))
                                                                      return imgString
                                                                      }


                                                                      Decoding



                                                                      func ConvertBase64StringToImage (imageBase64String:String) -> UIImage {
                                                                      let imageData = Data.init(base64Encoded: imageBase64String, options: .init(rawValue: 0))
                                                                      let image = UIImage(data: imageData!)
                                                                      return image
                                                                      }


                                                                      Note: Tested in xcode 9.4.1






                                                                      share|improve this answer
























                                                                      • now wrong swift 4

                                                                        – Masum Biswas
                                                                        Sep 27 '18 at 11:16











                                                                      • Thank you for comment on my answer, Can you please up vote my answer, this is very helpful for me.

                                                                        – Vivek
                                                                        Sep 27 '18 at 14:17














                                                                      4












                                                                      4








                                                                      4







                                                                      Swift 4



                                                                      Encoding



                                                                      func ConvertImageToBase64String (img: UIImage) -> String {
                                                                      let imageData:NSData = UIImageJPEGRepresentation(img, 0.50)! as NSData //UIImagePNGRepresentation(img)
                                                                      let imgString = imageData.base64EncodedString(options: .init(rawValue: 0))
                                                                      return imgString
                                                                      }


                                                                      Decoding



                                                                      func ConvertBase64StringToImage (imageBase64String:String) -> UIImage {
                                                                      let imageData = Data.init(base64Encoded: imageBase64String, options: .init(rawValue: 0))
                                                                      let image = UIImage(data: imageData!)
                                                                      return image
                                                                      }


                                                                      Note: Tested in xcode 9.4.1






                                                                      share|improve this answer













                                                                      Swift 4



                                                                      Encoding



                                                                      func ConvertImageToBase64String (img: UIImage) -> String {
                                                                      let imageData:NSData = UIImageJPEGRepresentation(img, 0.50)! as NSData //UIImagePNGRepresentation(img)
                                                                      let imgString = imageData.base64EncodedString(options: .init(rawValue: 0))
                                                                      return imgString
                                                                      }


                                                                      Decoding



                                                                      func ConvertBase64StringToImage (imageBase64String:String) -> UIImage {
                                                                      let imageData = Data.init(base64Encoded: imageBase64String, options: .init(rawValue: 0))
                                                                      let image = UIImage(data: imageData!)
                                                                      return image
                                                                      }


                                                                      Note: Tested in xcode 9.4.1







                                                                      share|improve this answer












                                                                      share|improve this answer



                                                                      share|improve this answer










                                                                      answered Jul 18 '18 at 5:55









                                                                      VivekVivek

                                                                      1,386918




                                                                      1,386918













                                                                      • now wrong swift 4

                                                                        – Masum Biswas
                                                                        Sep 27 '18 at 11:16











                                                                      • Thank you for comment on my answer, Can you please up vote my answer, this is very helpful for me.

                                                                        – Vivek
                                                                        Sep 27 '18 at 14:17



















                                                                      • now wrong swift 4

                                                                        – Masum Biswas
                                                                        Sep 27 '18 at 11:16











                                                                      • Thank you for comment on my answer, Can you please up vote my answer, this is very helpful for me.

                                                                        – Vivek
                                                                        Sep 27 '18 at 14:17

















                                                                      now wrong swift 4

                                                                      – Masum Biswas
                                                                      Sep 27 '18 at 11:16





                                                                      now wrong swift 4

                                                                      – Masum Biswas
                                                                      Sep 27 '18 at 11:16













                                                                      Thank you for comment on my answer, Can you please up vote my answer, this is very helpful for me.

                                                                      – Vivek
                                                                      Sep 27 '18 at 14:17





                                                                      Thank you for comment on my answer, Can you please up vote my answer, this is very helpful for me.

                                                                      – Vivek
                                                                      Sep 27 '18 at 14:17











                                                                      1














                                                                      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)
                                                                      }
                                                                      }





                                                                      share|improve this answer




























                                                                        1














                                                                        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)
                                                                        }
                                                                        }





                                                                        share|improve this answer


























                                                                          1












                                                                          1








                                                                          1







                                                                          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)
                                                                          }
                                                                          }





                                                                          share|improve this answer













                                                                          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)
                                                                          }
                                                                          }






                                                                          share|improve this answer












                                                                          share|improve this answer



                                                                          share|improve this answer










                                                                          answered Oct 13 '15 at 8:47









                                                                          A.GA.G

                                                                          10.6k7152




                                                                          10.6k7152























                                                                              1














                                                                              Swift version - create base64 for image



                                                                              In my opinion Implicitly Unwrapped Optional in case of UIImagePNGRepresenatation() is not safe, so I recommend to use extension like below:



                                                                              extension UIImage {

                                                                              func toBase64() -> String? {
                                                                              let imageData = UIImagePNGRepresentation(self)
                                                                              return imageData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
                                                                              }
                                                                              }





                                                                              share|improve this answer




























                                                                                1














                                                                                Swift version - create base64 for image



                                                                                In my opinion Implicitly Unwrapped Optional in case of UIImagePNGRepresenatation() is not safe, so I recommend to use extension like below:



                                                                                extension UIImage {

                                                                                func toBase64() -> String? {
                                                                                let imageData = UIImagePNGRepresentation(self)
                                                                                return imageData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
                                                                                }
                                                                                }





                                                                                share|improve this answer


























                                                                                  1












                                                                                  1








                                                                                  1







                                                                                  Swift version - create base64 for image



                                                                                  In my opinion Implicitly Unwrapped Optional in case of UIImagePNGRepresenatation() is not safe, so I recommend to use extension like below:



                                                                                  extension UIImage {

                                                                                  func toBase64() -> String? {
                                                                                  let imageData = UIImagePNGRepresentation(self)
                                                                                  return imageData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
                                                                                  }
                                                                                  }





                                                                                  share|improve this answer













                                                                                  Swift version - create base64 for image



                                                                                  In my opinion Implicitly Unwrapped Optional in case of UIImagePNGRepresenatation() is not safe, so I recommend to use extension like below:



                                                                                  extension UIImage {

                                                                                  func toBase64() -> String? {
                                                                                  let imageData = UIImagePNGRepresentation(self)
                                                                                  return imageData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
                                                                                  }
                                                                                  }






                                                                                  share|improve this answer












                                                                                  share|improve this answer



                                                                                  share|improve this answer










                                                                                  answered Sep 7 '16 at 13:46









                                                                                  lukszarlukszar

                                                                                  81069




                                                                                  81069























                                                                                      1














                                                                                      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!

                                                                                      }





                                                                                      share|improve this answer




























                                                                                        1














                                                                                        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!

                                                                                        }





                                                                                        share|improve this answer


























                                                                                          1












                                                                                          1








                                                                                          1







                                                                                          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!

                                                                                          }





                                                                                          share|improve this answer













                                                                                          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!

                                                                                          }






                                                                                          share|improve this answer












                                                                                          share|improve this answer



                                                                                          share|improve this answer










                                                                                          answered Oct 19 '16 at 14:01









                                                                                          Maniganda saravananManiganda saravanan

                                                                                          1,42411528




                                                                                          1,42411528























                                                                                              1














                                                                                              Swift 4.2 | Xcode 10



                                                                                              extension UIImage {

                                                                                              /// EZSE: Returns base64 string
                                                                                              public var base64: String {
                                                                                              return self.jpegData(compressionQuality: 1.0)!.base64EncodedString()
                                                                                              }
                                                                                              }





                                                                                              share|improve this answer




























                                                                                                1














                                                                                                Swift 4.2 | Xcode 10



                                                                                                extension UIImage {

                                                                                                /// EZSE: Returns base64 string
                                                                                                public var base64: String {
                                                                                                return self.jpegData(compressionQuality: 1.0)!.base64EncodedString()
                                                                                                }
                                                                                                }





                                                                                                share|improve this answer


























                                                                                                  1












                                                                                                  1








                                                                                                  1







                                                                                                  Swift 4.2 | Xcode 10



                                                                                                  extension UIImage {

                                                                                                  /// EZSE: Returns base64 string
                                                                                                  public var base64: String {
                                                                                                  return self.jpegData(compressionQuality: 1.0)!.base64EncodedString()
                                                                                                  }
                                                                                                  }





                                                                                                  share|improve this answer













                                                                                                  Swift 4.2 | Xcode 10



                                                                                                  extension UIImage {

                                                                                                  /// EZSE: Returns base64 string
                                                                                                  public var base64: String {
                                                                                                  return self.jpegData(compressionQuality: 1.0)!.base64EncodedString()
                                                                                                  }
                                                                                                  }






                                                                                                  share|improve this answer












                                                                                                  share|improve this answer



                                                                                                  share|improve this answer










                                                                                                  answered Oct 5 '18 at 13:03









                                                                                                  Ashish KakkadAshish Kakkad

                                                                                                  18.4k869109




                                                                                                  18.4k869109























                                                                                                      1














                                                                                                      Swift 4



                                                                                                      enum ImageFormat {
                                                                                                      case png
                                                                                                      case jpeg(CGFloat)
                                                                                                      }

                                                                                                      extension UIImage {
                                                                                                      func base64(format: ImageFormat) -> String? {
                                                                                                      var imageData: Data?

                                                                                                      switch format {
                                                                                                      case .png: imageData = UIImagePNGRepresentation(self)
                                                                                                      case .jpeg(let compression): imageData = UIImageJPEGRepresentation(self, compression)
                                                                                                      }

                                                                                                      return imageData?.base64EncodedString()
                                                                                                      }
                                                                                                      }

                                                                                                      extension String {
                                                                                                      func imageFromBase64() -> UIImage? {
                                                                                                      guard let data = Data(base64Encoded: self) else { return nil }

                                                                                                      return UIImage(data: data)
                                                                                                      }
                                                                                                      }





                                                                                                      share|improve this answer






























                                                                                                        1














                                                                                                        Swift 4



                                                                                                        enum ImageFormat {
                                                                                                        case png
                                                                                                        case jpeg(CGFloat)
                                                                                                        }

                                                                                                        extension UIImage {
                                                                                                        func base64(format: ImageFormat) -> String? {
                                                                                                        var imageData: Data?

                                                                                                        switch format {
                                                                                                        case .png: imageData = UIImagePNGRepresentation(self)
                                                                                                        case .jpeg(let compression): imageData = UIImageJPEGRepresentation(self, compression)
                                                                                                        }

                                                                                                        return imageData?.base64EncodedString()
                                                                                                        }
                                                                                                        }

                                                                                                        extension String {
                                                                                                        func imageFromBase64() -> UIImage? {
                                                                                                        guard let data = Data(base64Encoded: self) else { return nil }

                                                                                                        return UIImage(data: data)
                                                                                                        }
                                                                                                        }





                                                                                                        share|improve this answer




























                                                                                                          1












                                                                                                          1








                                                                                                          1







                                                                                                          Swift 4



                                                                                                          enum ImageFormat {
                                                                                                          case png
                                                                                                          case jpeg(CGFloat)
                                                                                                          }

                                                                                                          extension UIImage {
                                                                                                          func base64(format: ImageFormat) -> String? {
                                                                                                          var imageData: Data?

                                                                                                          switch format {
                                                                                                          case .png: imageData = UIImagePNGRepresentation(self)
                                                                                                          case .jpeg(let compression): imageData = UIImageJPEGRepresentation(self, compression)
                                                                                                          }

                                                                                                          return imageData?.base64EncodedString()
                                                                                                          }
                                                                                                          }

                                                                                                          extension String {
                                                                                                          func imageFromBase64() -> UIImage? {
                                                                                                          guard let data = Data(base64Encoded: self) else { return nil }

                                                                                                          return UIImage(data: data)
                                                                                                          }
                                                                                                          }





                                                                                                          share|improve this answer















                                                                                                          Swift 4



                                                                                                          enum ImageFormat {
                                                                                                          case png
                                                                                                          case jpeg(CGFloat)
                                                                                                          }

                                                                                                          extension UIImage {
                                                                                                          func base64(format: ImageFormat) -> String? {
                                                                                                          var imageData: Data?

                                                                                                          switch format {
                                                                                                          case .png: imageData = UIImagePNGRepresentation(self)
                                                                                                          case .jpeg(let compression): imageData = UIImageJPEGRepresentation(self, compression)
                                                                                                          }

                                                                                                          return imageData?.base64EncodedString()
                                                                                                          }
                                                                                                          }

                                                                                                          extension String {
                                                                                                          func imageFromBase64() -> UIImage? {
                                                                                                          guard let data = Data(base64Encoded: self) else { return nil }

                                                                                                          return UIImage(data: data)
                                                                                                          }
                                                                                                          }






                                                                                                          share|improve this answer














                                                                                                          share|improve this answer



                                                                                                          share|improve this answer








                                                                                                          edited Nov 21 '18 at 20:24

























                                                                                                          answered Nov 21 '18 at 20:00









                                                                                                          oscarroscarr

                                                                                                          1394




                                                                                                          1394























                                                                                                              0














                                                                                                              For iOS 7+, Objective-C, here's how to make the conversion starting with an image URL:



                                                                                                              NSURL *url = [NSURL URLWithString:self.groove.thumbnailURL];

                                                                                                              UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

                                                                                                              NSString *base64String = [UIImagePNGRepresentation(image)
                                                                                                              base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];





                                                                                                              share|improve this answer






























                                                                                                                0














                                                                                                                For iOS 7+, Objective-C, here's how to make the conversion starting with an image URL:



                                                                                                                NSURL *url = [NSURL URLWithString:self.groove.thumbnailURL];

                                                                                                                UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

                                                                                                                NSString *base64String = [UIImagePNGRepresentation(image)
                                                                                                                base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];





                                                                                                                share|improve this answer




























                                                                                                                  0












                                                                                                                  0








                                                                                                                  0







                                                                                                                  For iOS 7+, Objective-C, here's how to make the conversion starting with an image URL:



                                                                                                                  NSURL *url = [NSURL URLWithString:self.groove.thumbnailURL];

                                                                                                                  UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

                                                                                                                  NSString *base64String = [UIImagePNGRepresentation(image)
                                                                                                                  base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];





                                                                                                                  share|improve this answer















                                                                                                                  For iOS 7+, Objective-C, here's how to make the conversion starting with an image URL:



                                                                                                                  NSURL *url = [NSURL URLWithString:self.groove.thumbnailURL];

                                                                                                                  UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

                                                                                                                  NSString *base64String = [UIImagePNGRepresentation(image)
                                                                                                                  base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];






                                                                                                                  share|improve this answer














                                                                                                                  share|improve this answer



                                                                                                                  share|improve this answer








                                                                                                                  edited Sep 8 '15 at 8:44









                                                                                                                  EI Captain v2.0

                                                                                                                  18.4k96184




                                                                                                                  18.4k96184










                                                                                                                  answered Dec 24 '14 at 0:50









                                                                                                                  snibbesnibbe

                                                                                                                  2,16412029




                                                                                                                  2,16412029























                                                                                                                      0














                                                                                                                      Swift 3.0 and Xcode 8.0



                                                                                                                      let imageData = UIImageJPEGRepresentation(imageView.image!, 1)

                                                                                                                      let base64String = (imageData! as Data).base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
                                                                                                                      print(base64String)





                                                                                                                      share|improve this answer




























                                                                                                                        0














                                                                                                                        Swift 3.0 and Xcode 8.0



                                                                                                                        let imageData = UIImageJPEGRepresentation(imageView.image!, 1)

                                                                                                                        let base64String = (imageData! as Data).base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
                                                                                                                        print(base64String)





                                                                                                                        share|improve this answer


























                                                                                                                          0












                                                                                                                          0








                                                                                                                          0







                                                                                                                          Swift 3.0 and Xcode 8.0



                                                                                                                          let imageData = UIImageJPEGRepresentation(imageView.image!, 1)

                                                                                                                          let base64String = (imageData! as Data).base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
                                                                                                                          print(base64String)





                                                                                                                          share|improve this answer













                                                                                                                          Swift 3.0 and Xcode 8.0



                                                                                                                          let imageData = UIImageJPEGRepresentation(imageView.image!, 1)

                                                                                                                          let base64String = (imageData! as Data).base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
                                                                                                                          print(base64String)






                                                                                                                          share|improve this answer












                                                                                                                          share|improve this answer



                                                                                                                          share|improve this answer










                                                                                                                          answered Jan 9 '17 at 11:20









                                                                                                                          Amit VermaAmit Verma

                                                                                                                          78047




                                                                                                                          78047























                                                                                                                              0














                                                                                                                              I tried all the solutions, none worked for me (using Swift 4), this is the solution that worked for me, if anyone in future faces the same problem.



                                                                                                                              let temp = base64String.components(separatedBy: ",")
                                                                                                                              let dataDecoded : Data = Data(base64Encoded: temp[1], options:
                                                                                                                              .ignoreUnknownCharacters)!
                                                                                                                              let decodedimage = UIImage(data: dataDecoded)

                                                                                                                              yourImage.image = decodedimage





                                                                                                                              share|improve this answer




























                                                                                                                                0














                                                                                                                                I tried all the solutions, none worked for me (using Swift 4), this is the solution that worked for me, if anyone in future faces the same problem.



                                                                                                                                let temp = base64String.components(separatedBy: ",")
                                                                                                                                let dataDecoded : Data = Data(base64Encoded: temp[1], options:
                                                                                                                                .ignoreUnknownCharacters)!
                                                                                                                                let decodedimage = UIImage(data: dataDecoded)

                                                                                                                                yourImage.image = decodedimage





                                                                                                                                share|improve this answer


























                                                                                                                                  0












                                                                                                                                  0








                                                                                                                                  0







                                                                                                                                  I tried all the solutions, none worked for me (using Swift 4), this is the solution that worked for me, if anyone in future faces the same problem.



                                                                                                                                  let temp = base64String.components(separatedBy: ",")
                                                                                                                                  let dataDecoded : Data = Data(base64Encoded: temp[1], options:
                                                                                                                                  .ignoreUnknownCharacters)!
                                                                                                                                  let decodedimage = UIImage(data: dataDecoded)

                                                                                                                                  yourImage.image = decodedimage





                                                                                                                                  share|improve this answer













                                                                                                                                  I tried all the solutions, none worked for me (using Swift 4), this is the solution that worked for me, if anyone in future faces the same problem.



                                                                                                                                  let temp = base64String.components(separatedBy: ",")
                                                                                                                                  let dataDecoded : Data = Data(base64Encoded: temp[1], options:
                                                                                                                                  .ignoreUnknownCharacters)!
                                                                                                                                  let decodedimage = UIImage(data: dataDecoded)

                                                                                                                                  yourImage.image = decodedimage






                                                                                                                                  share|improve this answer












                                                                                                                                  share|improve this answer



                                                                                                                                  share|improve this answer










                                                                                                                                  answered Jul 19 '18 at 15:51









                                                                                                                                  ZubairZubair

                                                                                                                                  300412




                                                                                                                                  300412























                                                                                                                                      0














                                                                                                                                      Swift 4.2, Xcode 10.1



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



                                                                                                                                      print(imageData)






                                                                                                                                      share|improve this answer




























                                                                                                                                        0














                                                                                                                                        Swift 4.2, Xcode 10.1



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



                                                                                                                                        print(imageData)






                                                                                                                                        share|improve this answer


























                                                                                                                                          0












                                                                                                                                          0








                                                                                                                                          0







                                                                                                                                          Swift 4.2, Xcode 10.1



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



                                                                                                                                          print(imageData)






                                                                                                                                          share|improve this answer













                                                                                                                                          Swift 4.2, Xcode 10.1



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



                                                                                                                                          print(imageData)







                                                                                                                                          share|improve this answer












                                                                                                                                          share|improve this answer



                                                                                                                                          share|improve this answer










                                                                                                                                          answered Dec 13 '18 at 8:45









                                                                                                                                          Kedar SukerkarKedar Sukerkar

                                                                                                                                          212




                                                                                                                                          212






























                                                                                                                                              draft saved

                                                                                                                                              draft discarded




















































                                                                                                                                              Thanks for contributing an answer to Stack Overflow!


                                                                                                                                              • Please be sure to answer the question. Provide details and share your research!

                                                                                                                                              But avoid



                                                                                                                                              • Asking for help, clarification, or responding to other answers.

                                                                                                                                              • Making statements based on opinion; back them up with references or personal experience.


                                                                                                                                              To learn more, see our tips on writing great answers.




                                                                                                                                              draft saved


                                                                                                                                              draft discarded














                                                                                                                                              StackExchange.ready(
                                                                                                                                              function () {
                                                                                                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11251340%2fconvert-between-uiimage-and-base64-string%23new-answer', 'question_page');
                                                                                                                                              }
                                                                                                                                              );

                                                                                                                                              Post as a guest















                                                                                                                                              Required, but never shown





















































                                                                                                                                              Required, but never shown














                                                                                                                                              Required, but never shown












                                                                                                                                              Required, but never shown







                                                                                                                                              Required, but never shown

































                                                                                                                                              Required, but never shown














                                                                                                                                              Required, but never shown












                                                                                                                                              Required, but never shown







                                                                                                                                              Required, but never shown







                                                                                                                                              Popular posts from this blog

                                                                                                                                              MongoDB - Not Authorized To Execute Command

                                                                                                                                              How to fix TextFormField cause rebuild widget in Flutter

                                                                                                                                              Npm cannot find a required file even through it is in the searched directory