目标c-将图像文件写入ip地址



专家:

我整个上午都在研究如何通过IP地址将图像文件写入另一台计算机。我真的需要创建套接字、设置委托、安排运行循环、检查可用空间等等吗?真正地我不能使用IP地址和默认端口将文件保存到URL吗?

我问这个问题是因为我自己没有取得多大进展,我知道当我点击"发布你的问题"时,我会发现一些有用的信息。但如果没有发生这种情况,请回复。感谢您的帮助。

这是我的代码,我认为我走错了轨道:

#import "TESTtcpController.h"
@interface TESTtcpController()
@property (nonatomic, strong)NSInputStream *inputStream;
@property (nonatomic, strong)NSOutputStream *outputStream;
//void CFStreamCreatePairWithSocketToHost (
//                                         CFAllocatorRef alloc,
//                                         CFStringRef host,
//                                         UInt32 port,
//                                         CFReadStreamRef *readStream,
//                                         CFWriteStreamRef *writeStream
//                                         );
@end
@implementation TESTtcpController
+ (void)sendFile:(UIImage *)image
{
    UInt32 port = 80;
    NSString *ipAddress = @"10.10.10.10";
    TESTtcpController *tcpController = [[TESTtcpController alloc] init];
    [tcpController connect:port ipAddress:ipAddress];
    [tcpController postFile:image];
    [tcpController disconnect];
    tcpController = nil;
}
-(void)connect:(UInt32)port ipAddress:(NSString *)ipAddress
{
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)ipAddress, port, &readStream, &writeStream);
    self.inputStream = (__bridge NSInputStream *)readStream;
    self.outputStream = (__bridge NSOutputStream *)writeStream;
    [self.inputStream setDelegate:self];
    [self.outputStream setDelegate:self];
    [self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [self.inputStream open];
    [self.outputStream open];
    NSLog(@"input stream id %@", self.inputStream);
    /* Store a reference to the input and output streams so that
     they don't go away.... */
}
- (void)postFile:(UIImage *)image
{
}
- (void)dataSending:(NSString*)data {
    if(self.outputStream) {
        if(![self.outputStream hasSpaceAvailable])
            return;
        NSData *_data=[data dataUsingEncoding:NSUTF8StringEncoding]; int data_len = [_data length];
        uint8_t *readBytes = (uint8_t *)[_data bytes];
        int byteIndex=0;
        unsigned int len=0;
        while (TRUE) {
            len = ((data_len - byteIndex >= 40960) ? 40960 : (data_len-byteIndex));
            if(len==0)
                break;
            uint8_t buf[len]; (void)memcpy(buf, readBytes, len);
            len = [self.outputStream write:(const uint8_t *)buf maxLength:len]; byteIndex += len;
            readBytes += len; }
    }
}
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode
{
    switch(eventCode) {
        case NSStreamEventNone:
            break;
        case NSStreamEventOpenCompleted:
            break;
        case NSStreamEventHasBytesAvailable:
            break;
        case NSStreamEventHasSpaceAvailable:
//        {
//            uint8_t *readBytes = (uint8_t *)[_data mutableBytes];
//            readBytes += byteIndex; // instance variable to move pointer
//            int data_len = [_data length];
//            unsigned int len = ((data_len - byteIndex >= 1024) ?
//                                1024 : (data_len-byteIndex));
//            uint8_t buf[len];
//            (void)memcpy(buf, readBytes, len);
//            len = [stream write:(const uint8_t *)buf maxLength:len];
//            byteIndex += len;
//            break;
//        }
        case NSStreamEventErrorOccurred:
            break;
        case NSStreamEventEndEncountered:
            break;
            // continued ...
    }
}

-(void)disconnect{
    NSLog(@"disconnect method called");
    NSStreamStatus socketStatus = [self.outputStream streamStatus];
    int status = socketStatus;
    NSLog(@"Stream Status is %i", status);
    if (status == 2) {
        [self.inputStream close];
        [self.outputStream close];
        NSLog(@"Socket Closed");
    }
}
- (void)deallocMe
{
    [TESTtcpController dealloc];
}
@end

您应该使用AFNetworking库。图像上传将是微不足道的。点击此处了解AFNetworking:http://cocoadocs.org/docsets/AFNetworking/2.0.0/

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"Success: %@ %@", response, responseObject);
    }
}];
[uploadTask resume];

相关内容

  • 没有找到相关文章

最新更新