gpt4 book ai didi

iphone - 使用套接字流将图像或视频发送到服务器

转载 作者:行者123 更新时间:2023-12-03 11:56:12 24 4
gpt4 key购买 nike

我成功打开一个套接字向其发送数据。当我尝试发送图像时,它崩溃了,当我向其发送字符串时,它工作正常。我正在将图像转换为数据,然后将此数据放入必须为字符串的字符串中。发送到服务器。

需要指导,请帮忙

下面是代码,我用来连接流。

NSString *urlStr = @"http://182.71.22.107:1935/VideoCalling/5d14a9bc-b816-4c82-bbb7-623d18243a02.sdp/playlist.m3u8";
if (![urlStr isEqualToString:@""])
{
NSURL *website = [NSURL URLWithString:urlStr];
if (!website)
{
NSLog(@"%@ is not a valid URL");
return;
}

NSHost *host = [NSHost hostWithName:[website host]];
// iStream and oStream are instance variables
[NSStream getStreamsToHost:host port:8081 inputStream:&iStream outputStream:&oStream];
[iStream retain];
[oStream retain];
[iStream setDelegate:self];
[oStream setDelegate:self];
NSData *data = UIImageJPEGRepresentation([UIImage imageNamed:@"abc.png"], 90);
// Convert from host to network endianness
uint32_t length = (uint32_t)htonl([data length]);
// Don't forget to check the return value of 'write'
[oStream write:(uint8_t *)&length maxLength:4];
[oStream write:[data bytes] maxLength:length];//writes to stream
[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode];
[iStream open];
[oStream open];
}

我在这里写到流
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
int byteIndex;
switch(eventCode) {
case NSStreamEventHasSpaceAvailable:{
if (stream == oStream) {
//NSString * str = [NSString stringWithFormat:@"sdsdfdfggghhfhfh"];
NSString * str = [[NSString alloc]initWithData:datap encoding:NSUTF16StringEncoding];
NSLog(@"%@,lenght===%d",str,[str length]);
const uint8_t * rawstring = (const uint8_t *)[str UTF8String];
[oStream write:rawstring maxLength:strlen(15)];
[oStream close];
}

UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"h" message:@"Available" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
[a show];
[a release];
}
break;

最佳答案

关于iphone - 使用套接字流将图像或视频发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7525528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com