发送第一个UIImage后iOS应用程序崩溃

时间:2014-03-25 11:34:46

标签: ios

我正在开发一个iOS应用程序,它通过TCP套接字连接发送的不断流程的屏幕截图来与其他设备共享其屏幕。

这是我到目前为止编写的代码:

    @implementation newClient {
    NSData *data;

    NSInputStream *iStream;
    NSOutputStream *oStream;

    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;

    BOOL boolean;
}

-(void) connectToServerUsingCFStream:(UIImage *) bitmap {

    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault,
                                       (CFStringRef) @"192.168.1.9",
                                       8080,
                                       &readStream,
                                       &writeStream);

    if (readStream && writeStream) {
        CFReadStreamSetProperty(readStream,
                                kCFStreamPropertyShouldCloseNativeSocket,
                                kCFBooleanTrue);
        CFWriteStreamSetProperty(writeStream,
                                 kCFStreamPropertyShouldCloseNativeSocket,
                                 kCFBooleanTrue);

        iStream = (NSInputStream *)CFBridgingRelease(readStream);
        [iStream setDelegate:self];
        //[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
        //                   forMode:NSDefaultRunLoopMode];
        [iStream open];

        oStream = (NSOutputStream *)CFBridgingRelease(writeStream);
        [oStream setDelegate:self];
        [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                           forMode:NSDefaultRunLoopMode];
        [oStream open];
       // data = UIImageJPEGRepresentation(bitmap, 50);
        boolean = YES;
        [self writeToServer:bitmap];
    }
}

- (void) writeToServer:(UIImage *)bitmap {
    dispatch_async(dispatch_get_main_queue(), ^{
        @autoreleasepool {
            NSData *data = UIImageJPEGRepresentation(bitmap, 50);
            int len;
            uint32_t length = (uint32_t)htonl([data length]);
            // Sending out the length of the screenshot image and then the image
            len=[oStream write:(const uint8_t *)&length maxLength:4];
            NSLog(@"len=%d",len);
            len=[oStream write:(const uint8_t *)[data bytes] maxLength:[data length]];
            NSLog(@"len=%d",len);
            //[oStream write:(const uint8_t *)"/n" maxLength:0];
        }
    });
}
@end

当我点击激活此对象的按钮时,发送第一个屏幕截图,然后崩溃,在我的项目的main.m文件中给出了"线程1:EXC_BAD_ACCESS(代码= 1,地址= 0xd000000c)。

服务器应用程序在Android上运行,它是一个不断循环运行的线程。

我不知道导致问题的原因。

0 个答案:

没有答案