0

On my iOS side, i received a stream of images via NSInputStream. NSMutableData will be containing images in [JPG IMAGE][JPG IMAGE][PNG IMAGE].

Is there any elegant way of getting the images from NSMutableData?

EDIT: Thanks for all the replies. I do not know the start/end of the images. I converted the buffer to NSData and append it to NSMutableData. Do i need to include a delimiter in between the images?

Server(.Net): Continuous sending images, so on the client side (iOS) i will never encounter NSStreamEvent.EndEncountered . Closing the NetoworkStream will throw me ObjectDisposedException was unhandled, cannot access a disposed object (fixing it now)

Client: Does not know when the server has completed sending, all it has is a NSMutableData. (fixing it now) I convert the buffer to NSData and append it to NSMutableData

Basically what i want to achieve is: 1. I press connect on my client (iOS app), 2. Establish connection with the server (.Net) 3. Server create and send images to client 4. Client displays the images. 5. Connect and Disconnect initiated by client.

Update: I got it working via: 1. At server side, after each image sent, i append a delimeter. 2. At client side, i append the data to NSMutableData as usual and check the last few bytes for delimeter. If it matches, i can safely proceed to chunk the NSMutableData by the delimeter.

via that, i am able to receive & display multiple images!

perwyl
  • 323
  • 3
  • 14
  • 1
    `-[UIImage imageWithData:]`? https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/ – Nicolas Miari Sep 03 '15 at 09:36
  • So there are multiple images all together in the data? How do you know where each image starts and ends? Or was the original data and encoded array containing image data? Not really clear what you're asking... – Wain Sep 03 '15 at 09:41
  • You can look for this: http://stackoverflow.com/questions/30481563/getting-frames-though-a-stream-and-display-on-screen/30482694#30482694 and modify it to check also for possible PNG images (check the start/end of files: http://www.w3.org/TR/PNG-Structure.html) – Larme Sep 03 '15 at 09:55

3 Answers3

1

You can use

[UIImage imageWithData:[yourMutableData copy]]

copy is not necessary. It makes your mutable data to immutable and you got copy.

Candost
  • 1,029
  • 1
  • 12
  • 28
0

You Can Use

NSData *data = yourData;

UIImage *image = [UIImage imageWithData:data];

Avijit Nagare
  • 8,482
  • 7
  • 39
  • 68
0

It is possible, but it will be quite tricky in your case - because you indicated that the NSMutableData will contain more than one type of images (JPEG/PNG).

If we assume, that the data arrived in good format, the best way to explore and separate it is by the file signature:

  1. For PNG it's the first 8 bytes
  2. For JPEG it's the first 4 bytes

Incorporate this to separate the huge blob of data into an array of data chunks. You'll have to go byte by byte and look for subsequences and check if the subsequence happens to be of a JPEG or PNG file. Then simply initialize your UIImage objects like this:

var dataArray: [NSMutableData] = [imageData1, imageData2, imageData3]
var imageArray: [UIImage] = []

for imgData in dataArray {
    if let image = UIImage(data: imgData) {
        imageArray.append(image) 
    }
}
Michal
  • 15,429
  • 10
  • 73
  • 104