Sending UIImage over NSOutputStream [closed]
Asked Answered
J

2

9

I'm trying to send an image that user takes to the server. I get the JPEG representation, append that to the string needed to upload the photo, and then send the data through NSOutputStream. However, when I try to get the photo back from server, I only see 10% of it from the top. Any help would be appreciated.

btw. socket is open and connected.

Here's my code:

NSString *requestString = [NSString stringWithFormat:@"SubmitPhoto::%@::", userID];
NSData * stringData = [requestString dataUsingEncoding:NSUTF8StringEncoding];

NSData *imgData = UIImageJPEGRepresentation(image, 1.0);

NSMutableData *completeData = [[NSMutableData alloc] initWithBytes:[stringData bytes] length:[stringData length]];
[completeData appendData:imgData];

//sending NSData over to server
[self.outputStream write:[completeData bytes] maxLength:[completeData length]];
Jezabel answered 20/3, 2013 at 14:52 Comment(4)
have you tried different JPEG Compression? like (image, 0.4f)Lanam
I wonder if it's size limit threshold is being exceededLanam
Can you try to write the image to memory (buffer) and attempt to render the recovered image? I tried to do this in memory and it worked fine for a 1024 x 683 photo. Here's some sample code: gist.github.com/abellina/5205633.Charpoy
Thank you both for quick responses. I brought down the size of the photo and it is working.Jezabel
E
5

It's because of your image size limit is being exceeded.

Better way to handle this is to implement the following logic.

Sender

  1. Convert UIimage to NSData

  2. Split up the NSData to different chunks (1024 per chunk is recommended)

  3. Send & track each chunk of NSData

Receiver

  1. Declare NSData and Store the first part of NSData chunk (1024) into it, which is received.

  2. Receive the next chunks of NSData and make use appendData: method to append it

  3. Once all the chunks are received, convert the received NSData as an UIImage

Make sure to design different structures for transferring the data as chunks such as structure to represent the details (total chunk, total size, chunk size etc..), structure to represent the data (current chunk size, current chunk number etc..), structure to represent the responds data (delivery status,chunk number delivered etc..).

Edee answered 20/3, 2013 at 17:11 Comment(0)
E
3

I'd guess you're just trying to write too much data at a time for your buffer. Do something like this to loop over the data and send it in chunks instead:

    NSString *requestString = [NSString stringWithFormat:@"SubmitPhoto::%@::", userID];
    NSData * stringData = [requestString dataUsingEncoding:NSUTF8StringEncoding];

    NSData *imgData = UIImageJPEGRepresentation(image, 1.0);

    NSMutableData *completeData = [[NSMutableData alloc] initWithBytes:[stringData bytes] length:[stringData length]];
    [completeData appendData:imgData];

    NSInteger bytesWritten = 0;
    while ( completeData.length > bytesWritten )
    {
        while ( ! self.outputStream.hasSpaceAvailable )
            [NSThread sleepForTimeInterval:0.05];

        //sending NSData over to server
        NSInteger writeResult = [self.outputStream write:[completeData bytes]+bytesWritten maxLength:[completeData length]-bytesWritten];
        if ( writeResult == -1 ) {
            NSLog(@"error code here");
        }
        else {
            bytesWritten += writeResult;
        }
    }
}
// Both input and output should be closed to make the code work in swift
Earshot answered 20/3, 2013 at 16:28 Comment(1)
Wonderful. I'm going to use that now!Brummett

© 2022 - 2024 — McMap. All rights reserved.