iosimagensstreamnsinputstreamnsoutputstream

Transfer Multiple Images via NSInputStream/NSOutputStream


I have two NSInputStream and NSOutputStream connected to each other via network. I want to transfer core data objects and associated images from one device to other device. I have successfully converted the core data objects into JSON and transferred to other side of the stream and then populated the core data from the JSON. Now there are images that are associated with each recorded. The images are on the disc, there is only path stored in core data objects. Now, you have to have complete data at hand when you are writing to the output stream. I have the XML (that contains JSON) ready.
1. But how do I transfer images (NSData *) along with XML (also NSData *) ? How will I differentiate at the reading end (NSInputStream) between XML and Images?
2. Also, I have to transfer multiple images, how do we tell at the NSInputStream end that the bytes of an image have finished and the the bytes of next image have started ?
3. How do we know that which image (name) has been transferred ?
Thanks


Solution

  • I solved it by using following steps:
    1. Convert each managed object to NSDictionary
    2. Put all all dictionaries in NSArray
    3. Convert the NSArray to NSData using NSKeyedArchiver
    4. Transfer NSData through streams

    And at receiver's end, I reversed above steps.
    Thanks Marius Kurgonas