Sending UIImage over NSOutputStream [closed]

谁都会走 提交于 2019-12-04 03:58:41

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

escrafford

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!