iOS:如何通过套接字定期发送图像数据?

时间:2014-02-03 18:12:45

标签: ios sockets

我可以将我的iOS程序连接到基于java的套接字侦听器,并使其连接良好。 iOS应用程序还能够很好地将屏幕截图图像数据发送到套接字。这是在NSStreamEventHasSpaceAvailable

下完成的

我的问题是,我不知道,如何定期将此屏幕截图发送到套接字。我现在只发一次,它的工作。但是,我想继续将这个图像数据一个接一个地发送到套接字,并在每个特定的时间间隔内以编程方式截取。怎么做?请指教。

请参阅下面的完整代码。

-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
    NSString *io;
    if (theStream == inputStream) io = @">>";
    else io = @"<<";

    NSString *event;
    switch (streamEvent)
    {
        case NSStreamEventNone:
            event = @"NSStreamEventNone";
            //statusText.text =  @"Can not connect to the host!";
            NSLog(@"NSStreamEventNone - Can not connect to the host");
            break;

        case NSStreamEventOpenCompleted:
            event = @"NSStreamEventOpenCompleted";
            //pingButton.hidden = NO;
            //statusText.text = @"Connected";
            NSLog(@"Connected");
            break;

        case NSStreamEventHasBytesAvailable:
            event = @"NSStreamEventHasBytesAvailable";
            NSLog(@"NSStreamEventHasBytesAvailable called");
            if (theStream == inputStream)
            {
                //read data
                uint8_t buffer[1024];
                int len;
                while ([inputStream hasBytesAvailable])
                {
                    len = [inputStream read:buffer maxLength:sizeof(buffer)];
                    if (len > 0)
                    {
                        NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
                        NSData *theData = [[NSData alloc] initWithBytes:buffer length:len];
                        if (nil != output)
                        {
                            //do something with data
                            NSLog(@"NSStreamEventHasBytesAvailable theData: %@", theData);
                        }
                    }
                }
            }
            break;

        case NSStreamEventHasSpaceAvailable:
            event = @"NSStreamEventHasSpaceAvailable";

            NSLog(@"NSStreamEventHasSpaceAvailable called");

            NSLog(@"space : %d",[outputStream hasSpaceAvailable]);

            if (theStream == outputStream)
        {
                 AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
                UIGraphicsBeginImageContext(appDelegate.window.bounds.size);
                [appDelegate.window.layer renderInContext:UIGraphicsGetCurrentContext()];
                UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
                NSData * data = UIImagePNGRepresentation(image);
                [data writeToFile:@"screenshot.png" atomically:YES];

                int num = [outputStream write:[data bytes] maxLength:[data length]];
                if (-1 == num) {
                    NSLog(@"Error writing to stream %@: %@", outputStream, [outputStream streamError]);
                }else{
                    NSLog(@"Wrote %i bytes to stream %@.", num, outputStream);
                    //[outputStream close];
                }
        }
            break;

        case NSStreamEventErrorOccurred:
            event = @"NSStreamEventErrorOccurred";
            //statusText.text = @"Can not connect to the host!";
            //pingButton.hidden = YES;
            NSLog(@"NSStreamEventErrorOccurred - Can not connect to the host");
            break;

        case NSStreamEventEndEncountered:
            event = @"NSStreamEventEndEncountered";
            //statusText.text = @"Connection closed by the server.";
            //pingButton.hidden = YES;
            NSLog(@"NSStreamEventEndEncountered - Connection closed by the server");
            [theStream close];
            [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            //[theStream release];
            theStream = nil;
            break;

        default:
            event = @"** Unknown";
    }

    NSLog(@"%@ : %@", io, event);
}

在按钮单击下,以下代码最初用于连接主机。

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"localhost", 8080, &readStream, &writeStream);

inputStream = (__bridge_transfer NSInputStream *)readStream;
outputStream = (__bridge_transfer NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];

CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);

0 个答案:

没有答案