1

I want to convert a UIImage to an NSOutputStream and send it to a server through socket.


#import "Connection.h"

@implementation Connection

-(void) open: (NSString *) h : (int) p
{
    strHost = h;
    intPort = p;

    [NSStream getStreamsToHost:objHost
                port:intPort
            inputStream:&receiveStream
            outputStream:&sendStream];

    [receiveStream retain];
    [sendStream retain];

    [receiveStream setDelegate:self];
    [sendStream setDelegate:self];

    [receiveStream scheduleInRunLoop:[NSRunLoop currentRunLoop]                 forMode:NSDefaultRunLoopMode];
    [sendStream scheduleInRunLoop:[NSRunLoop currentRunLoop]                forMode:NSDefaultRunLoopMode];

    [receiveStream open];
    [sendStream open];

    printf("Open.\n");
}


- (void) stream: (NSStream *) stream handleEvent: (NSStreamEvent) eventCode
{
    printf("EVENT: Start.\n");

    switch(eventCode)
    {
        case NSStreamEventOpenCompleted:
        {
            printf("EVENT: Open completed.\n");

            if(stream == receiveStream)
            {
                printf("Receiving...\n");
            }

            if(stream == sendStream)
            {
                printf("Sending...\n");

                NSString * strBuffer = [NSString stringWithFormat:@"GET / HTTP/1.0\r\n\r\n"];
                const uint8_t * rawstring = (const uint8_t *)[strBuffer UTF8String];

                [sendStream write:rawstring maxLength:strlen(rawstring)];
            }

            break;
        }
        case NSStreamEventEndEncountered:
        {
            printf("EVENT: End encountered.\n");
            break;
        }
        case NSStreamEventHasSpaceAvailable:
        {
            printf("EVENT: Has space available.\n");
            break;
        }
        case NSStreamEventHasBytesAvailable:
        {
            printf("EVENT: Has bytes available.\n");
            break;
        }
        case NSStreamEventErrorOccurred:
        {
            printf("EVENT: Error occurred.\n");
            break;
        }
        case NSStreamEventNone:
        {
            printf("EVENT: None.\n");
            break;
        }
    }

    printf("EVENT: End.\n");
}

-(void) close
{
    [receiveStream close];
    [sendStream close];

    printf("Closed.\n");
}

@end


My question is where can I add code like "sendStream = ..."?

Another question is that I can convert UIImage to NSData using:

NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90);

But how to convert the imageData to NSOutputStream's instance?

Chilly Zhong
  • 16,763
  • 23
  • 77
  • 103
  • Is there any way to convert NSOutputStream to NSData? https://stackoverflow.com/questions/59526513/how-to-get-image-from-nsoutputstream-in-objective-c – Mihir Oza Dec 30 '19 at 13:01

1 Answers1

2

My question is where can I add code like "sendStream = ..."?

You're already assigning sendStream with the getStreamsToHost:port:inputStream:outputStream: message. That method returns the two streams by reference.

… how to convert the imageData to NSOutputStream's instance?

You don't need to convert the data to a stream, you need to tell a stream to write the data.

Try NSOutputStream's write:maxLength: method. You'll need to pass the bytes and length from the data object.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • 1
    You can use NSData's -bytes and -length messages for the last bit. – Jesse Rusak Mar 29 '09 at 13:40
  • sorry asked in chat but they didnt know and it seems like you would. im trying to stream audio data. currently i use gcdasyncsocket and write the nsdata to it. however i receive static so there is somthing wrong with the format. that data wouldnt be any different if i used nsstream would it? – owen gerig Jun 19 '12 at 20:18
  • @owengerig: It would only be different if the receiving code—either yours or GCDAsyncSocket's—is wrong. You have GCDAsyncSocket's source code, so sprinkle NSLogs in all the right places and see what it's receiving before it passes it along to you. My bet would be no. – Peter Hosey Jun 19 '12 at 23:28