IOS asyncSocket上传图片
2014-05-16 来自:静水流深 1 人回应
asyncSocket发送照片回服务器,点击发送button可以发送一张图片,点击多次button可以发送多张图片。但是想实现点击一次button循环发送多张图片,无法实现,只能发送最后一张。求大神帮忙!!! 
-(void)sendBtnClick:(id)sender
{
    _asyncSocket = nil;
    for (int i = 0; i<3; i++) {
        NSLog(@"第%d次",i);
        [self send];
    }
}
-(void)send
{
    if (!_asyncSocket)
    {
        _asyncSocket = [[AsyncSocket alloc]initWithDelegate:self];
        NSString *host = @"172.17.73.123";
        int nPort = 8484;
        NSError *error = nil;
        //[_asyncSocket connectToHost:host onPort:nPort error:&error];
        if ([_asyncSocket connectToHost:host onPort:nPort withTimeout:5 error:&error]) {
            NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"1.jpg"], 1);
            [_asyncSocket writeData:imageData withTimeout:-1 tag:1];
        }else{
            NSLog(@"无法连接,原因是:%@",error);
        }
    }
    sleep(5);
}
#pragma delegate
- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err
{
    NSLog(@"Info___willDisconnectWithError\n");
    NSLog(@"%@",err);
    [sock disconnectAfterWriting];
}

- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port { NSLog(@"Info___didConnectToHost\n"); NSLog(@"Socket did Connect to host: %@:%u", host, port); _asyncSocket = nil; [sock disconnectAfterWriting]; }


控制台输出情况:

 2014-04-19 10:30:40.820 TestSocket[980:a0b] 第0次
2014-04-19 10:30:40.824 TestSocket[980:a0b] Info___willConnectToHost
2014-04-19 10:30:45.859 TestSocket[980:a0b] 第1次
2014-04-19 10:30:50.860 TestSocket[980:a0b] 第2次
2014-04-19 10:30:55.863 TestSocket[980:a0b] Info___didConnectToHost
2014-04-19 10:30:55.864 TestSocket[980:a0b] Socket did Connect to host: 172.17.73.123:8484 
 
结果:服务器只收到一张图片
2014-05-22 来自:micsim

NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"1.jpg"], 1);  这行里的图片名称不应该固定死,否则你每次上传的都是1.jpg都会覆盖之前上传的。

您的回应

你还未登陆,不能回应!登陆