How do you include png files in a Swift object from a JSON file?












0















My phone is parsing data from an Xcode JSON file to objects from a Codable model and passing data to watch using WCSession. Everything works great.



JSON files only include URLs/paths to images not actual images obviously so how do I add images from my Xcode project directory as part of the same JSON?



I would add a local path to the JSON file pointing to a set of images? Then add a property let profile : UIImage to my data object? Will it be a separate struct data object? Would it be easier to use dummy images from live URLs? How would I actually pass the image to a UIImage/WKInterfaceImage in a table?






    struct MessageObject : Codable {

var title : String
//...

enum MessageKeys: String, CodingKey {
//...
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: MessageKeys.self)

self.title = try container.decode(String.self, forKey: .title)
//...
}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: MessageKeys.self)

//...
}
}









share|improve this question

























  • Thanks. I edited my question. I am already using that API successfully. I want to add separate images from my project directory. But include them in the same data model.

    – TokyoToo
    Nov 20 '18 at 6:51











  • Which of the two answers would most represent real-world?

    – TokyoToo
    Nov 20 '18 at 10:52
















0















My phone is parsing data from an Xcode JSON file to objects from a Codable model and passing data to watch using WCSession. Everything works great.



JSON files only include URLs/paths to images not actual images obviously so how do I add images from my Xcode project directory as part of the same JSON?



I would add a local path to the JSON file pointing to a set of images? Then add a property let profile : UIImage to my data object? Will it be a separate struct data object? Would it be easier to use dummy images from live URLs? How would I actually pass the image to a UIImage/WKInterfaceImage in a table?






    struct MessageObject : Codable {

var title : String
//...

enum MessageKeys: String, CodingKey {
//...
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: MessageKeys.self)

self.title = try container.decode(String.self, forKey: .title)
//...
}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: MessageKeys.self)

//...
}
}









share|improve this question

























  • Thanks. I edited my question. I am already using that API successfully. I want to add separate images from my project directory. But include them in the same data model.

    – TokyoToo
    Nov 20 '18 at 6:51











  • Which of the two answers would most represent real-world?

    – TokyoToo
    Nov 20 '18 at 10:52














0












0








0








My phone is parsing data from an Xcode JSON file to objects from a Codable model and passing data to watch using WCSession. Everything works great.



JSON files only include URLs/paths to images not actual images obviously so how do I add images from my Xcode project directory as part of the same JSON?



I would add a local path to the JSON file pointing to a set of images? Then add a property let profile : UIImage to my data object? Will it be a separate struct data object? Would it be easier to use dummy images from live URLs? How would I actually pass the image to a UIImage/WKInterfaceImage in a table?






    struct MessageObject : Codable {

var title : String
//...

enum MessageKeys: String, CodingKey {
//...
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: MessageKeys.self)

self.title = try container.decode(String.self, forKey: .title)
//...
}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: MessageKeys.self)

//...
}
}









share|improve this question
















My phone is parsing data from an Xcode JSON file to objects from a Codable model and passing data to watch using WCSession. Everything works great.



JSON files only include URLs/paths to images not actual images obviously so how do I add images from my Xcode project directory as part of the same JSON?



I would add a local path to the JSON file pointing to a set of images? Then add a property let profile : UIImage to my data object? Will it be a separate struct data object? Would it be easier to use dummy images from live URLs? How would I actually pass the image to a UIImage/WKInterfaceImage in a table?






    struct MessageObject : Codable {

var title : String
//...

enum MessageKeys: String, CodingKey {
//...
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: MessageKeys.self)

self.title = try container.decode(String.self, forKey: .title)
//...
}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: MessageKeys.self)

//...
}
}






json swift codable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 6:49







TokyoToo

















asked Nov 20 '18 at 6:44









TokyoTooTokyoToo

5731716




5731716













  • Thanks. I edited my question. I am already using that API successfully. I want to add separate images from my project directory. But include them in the same data model.

    – TokyoToo
    Nov 20 '18 at 6:51











  • Which of the two answers would most represent real-world?

    – TokyoToo
    Nov 20 '18 at 10:52



















  • Thanks. I edited my question. I am already using that API successfully. I want to add separate images from my project directory. But include them in the same data model.

    – TokyoToo
    Nov 20 '18 at 6:51











  • Which of the two answers would most represent real-world?

    – TokyoToo
    Nov 20 '18 at 10:52

















Thanks. I edited my question. I am already using that API successfully. I want to add separate images from my project directory. But include them in the same data model.

– TokyoToo
Nov 20 '18 at 6:51





Thanks. I edited my question. I am already using that API successfully. I want to add separate images from my project directory. But include them in the same data model.

– TokyoToo
Nov 20 '18 at 6:51













Which of the two answers would most represent real-world?

– TokyoToo
Nov 20 '18 at 10:52





Which of the two answers would most represent real-world?

– TokyoToo
Nov 20 '18 at 10:52












2 Answers
2






active

oldest

votes


















1














How about passing the Data of the image?



Your Codable model could include a property like this:



var imageData: Data


And then you could set it like this:



imageData = someUIImage.pngData()!


When you encode it, the JSON will become something like:



{"imageData":"iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAAXNSR0IArs4c6QAAABxpRE9UAAAAAgAAAAAAAAAEAAAAKAAAAAQAAAAEAAAAWxzQ72oAAAAnSURBVCgVYjhzeOX//1+uYGCQOAgz4JPEqgCmE0SDNKOYgC4JUgAAAAD//ytw2SEAAAAnSURBVGP4/+XKfxA+c3glHMPEQDQDPkmwAlw6YaYwwBTABJBpkBwAoM/kGdRps6oAAAAASUVORK5CYII="}


To get an image from the data, just do:



UIImage(data: imageData)





share|improve this answer































    0














    In case you'd like to create the JSON file beforehand (not on the fly) and your images are not meant to change, then you could convert your images to base64 encoded strings in a site like this one and then add those strings to the JSON file:



    {
    "image1": "iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHL..."
    "image2": "G2qoAEpaUQIZD4QYT4A62AHzQUqOiTgsgPEBCJIKAVJF..."
    ...
    }


    Creating a Data object and eventually a UIImage for each base64 encoded image later on would be similar to this:



    if let imageData = Data(base64Encoded: img1Base64) {        
    if let image = UIImage(data: imageData) {
    imageView.image = image
    }
    }





    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%2f53387587%2fhow-do-you-include-png-files-in-a-swift-object-from-a-json-file%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      How about passing the Data of the image?



      Your Codable model could include a property like this:



      var imageData: Data


      And then you could set it like this:



      imageData = someUIImage.pngData()!


      When you encode it, the JSON will become something like:



      {"imageData":"iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAAXNSR0IArs4c6QAAABxpRE9UAAAAAgAAAAAAAAAEAAAAKAAAAAQAAAAEAAAAWxzQ72oAAAAnSURBVCgVYjhzeOX//1+uYGCQOAgz4JPEqgCmE0SDNKOYgC4JUgAAAAD//ytw2SEAAAAnSURBVGP4/+XKfxA+c3glHMPEQDQDPkmwAlw6YaYwwBTABJBpkBwAoM/kGdRps6oAAAAASUVORK5CYII="}


      To get an image from the data, just do:



      UIImage(data: imageData)





      share|improve this answer




























        1














        How about passing the Data of the image?



        Your Codable model could include a property like this:



        var imageData: Data


        And then you could set it like this:



        imageData = someUIImage.pngData()!


        When you encode it, the JSON will become something like:



        {"imageData":"iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAAXNSR0IArs4c6QAAABxpRE9UAAAAAgAAAAAAAAAEAAAAKAAAAAQAAAAEAAAAWxzQ72oAAAAnSURBVCgVYjhzeOX//1+uYGCQOAgz4JPEqgCmE0SDNKOYgC4JUgAAAAD//ytw2SEAAAAnSURBVGP4/+XKfxA+c3glHMPEQDQDPkmwAlw6YaYwwBTABJBpkBwAoM/kGdRps6oAAAAASUVORK5CYII="}


        To get an image from the data, just do:



        UIImage(data: imageData)





        share|improve this answer


























          1












          1








          1







          How about passing the Data of the image?



          Your Codable model could include a property like this:



          var imageData: Data


          And then you could set it like this:



          imageData = someUIImage.pngData()!


          When you encode it, the JSON will become something like:



          {"imageData":"iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAAXNSR0IArs4c6QAAABxpRE9UAAAAAgAAAAAAAAAEAAAAKAAAAAQAAAAEAAAAWxzQ72oAAAAnSURBVCgVYjhzeOX//1+uYGCQOAgz4JPEqgCmE0SDNKOYgC4JUgAAAAD//ytw2SEAAAAnSURBVGP4/+XKfxA+c3glHMPEQDQDPkmwAlw6YaYwwBTABJBpkBwAoM/kGdRps6oAAAAASUVORK5CYII="}


          To get an image from the data, just do:



          UIImage(data: imageData)





          share|improve this answer













          How about passing the Data of the image?



          Your Codable model could include a property like this:



          var imageData: Data


          And then you could set it like this:



          imageData = someUIImage.pngData()!


          When you encode it, the JSON will become something like:



          {"imageData":"iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAAXNSR0IArs4c6QAAABxpRE9UAAAAAgAAAAAAAAAEAAAAKAAAAAQAAAAEAAAAWxzQ72oAAAAnSURBVCgVYjhzeOX//1+uYGCQOAgz4JPEqgCmE0SDNKOYgC4JUgAAAAD//ytw2SEAAAAnSURBVGP4/+XKfxA+c3glHMPEQDQDPkmwAlw6YaYwwBTABJBpkBwAoM/kGdRps6oAAAAASUVORK5CYII="}


          To get an image from the data, just do:



          UIImage(data: imageData)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 7:14









          SweeperSweeper

          68.3k1073140




          68.3k1073140

























              0














              In case you'd like to create the JSON file beforehand (not on the fly) and your images are not meant to change, then you could convert your images to base64 encoded strings in a site like this one and then add those strings to the JSON file:



              {
              "image1": "iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHL..."
              "image2": "G2qoAEpaUQIZD4QYT4A62AHzQUqOiTgsgPEBCJIKAVJF..."
              ...
              }


              Creating a Data object and eventually a UIImage for each base64 encoded image later on would be similar to this:



              if let imageData = Data(base64Encoded: img1Base64) {        
              if let image = UIImage(data: imageData) {
              imageView.image = image
              }
              }





              share|improve this answer




























                0














                In case you'd like to create the JSON file beforehand (not on the fly) and your images are not meant to change, then you could convert your images to base64 encoded strings in a site like this one and then add those strings to the JSON file:



                {
                "image1": "iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHL..."
                "image2": "G2qoAEpaUQIZD4QYT4A62AHzQUqOiTgsgPEBCJIKAVJF..."
                ...
                }


                Creating a Data object and eventually a UIImage for each base64 encoded image later on would be similar to this:



                if let imageData = Data(base64Encoded: img1Base64) {        
                if let image = UIImage(data: imageData) {
                imageView.image = image
                }
                }





                share|improve this answer


























                  0












                  0








                  0







                  In case you'd like to create the JSON file beforehand (not on the fly) and your images are not meant to change, then you could convert your images to base64 encoded strings in a site like this one and then add those strings to the JSON file:



                  {
                  "image1": "iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHL..."
                  "image2": "G2qoAEpaUQIZD4QYT4A62AHzQUqOiTgsgPEBCJIKAVJF..."
                  ...
                  }


                  Creating a Data object and eventually a UIImage for each base64 encoded image later on would be similar to this:



                  if let imageData = Data(base64Encoded: img1Base64) {        
                  if let image = UIImage(data: imageData) {
                  imageView.image = image
                  }
                  }





                  share|improve this answer













                  In case you'd like to create the JSON file beforehand (not on the fly) and your images are not meant to change, then you could convert your images to base64 encoded strings in a site like this one and then add those strings to the JSON file:



                  {
                  "image1": "iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHL..."
                  "image2": "G2qoAEpaUQIZD4QYT4A62AHzQUqOiTgsgPEBCJIKAVJF..."
                  ...
                  }


                  Creating a Data object and eventually a UIImage for each base64 encoded image later on would be similar to this:



                  if let imageData = Data(base64Encoded: img1Base64) {        
                  if let image = UIImage(data: imageData) {
                  imageView.image = image
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 7:37









                  Gabriel T.Gabriel T.

                  122




                  122






























                      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%2f53387587%2fhow-do-you-include-png-files-in-a-swift-object-from-a-json-file%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







                      這個網誌中的熱門文章

                      Xamarin.form Move up view when keyboard appear

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Anylogic : not able to use stopDelay()