multipart/x-mixed-replace with iPhone SDK
我正试图下载几个图像来响应一个http请求。在服务器端(java),我使用oreilly multipart response,在我的代理中调用didReceiveData(每个图像大约调用一次),然后在我的iPhone模拟器中获取数据(每个图像大约调用一次)。 问题是这大约。。。有没有人能用iphonesdk正确处理multipart/x-mixed-re?如果是,这里的最佳策略是什么?我应该玩预期的长度吗?在服务器端?在客户端?我应该等到我收到所有的东西。。。嗯,这还不够,因为对didReceiveData的调用是以随机顺序进行的(我在询问picture1,picture2,有时我会收到picture2,picture1,尽管服务器端尊重顺序!)。我是否应该在服务器端的图片之间进行调整? 或者我应该放弃multipart/x-mixed-replace吗?那么最简单的方法是什么multipart/x-mixed-replace with iPhone SDK,iphone,cocoa,http,sdk,nsurlconnection,Iphone,Cocoa,Http,Sdk,Nsurlconnection,我正试图下载几个图像来响应一个http请求。在服务器端(java),我使用oreilly multipart response,在我的代理中调用didReceiveData(每个图像大约调用一次),然后在我的iPhone模拟器中获取数据(每个图像大约调用一次)。 问题是这大约。。。有没有人能用iphonesdk正确处理multipart/x-mixed-re?如果是,这里的最佳策略是什么?我应该玩预期的长度吗?在服务器端?在客户端?我应该等到我收到所有的东西。。。嗯,这还不够,因为对didRec
有很多问题,但我真的被困在这里了!谢谢你的帮助 我不确定您对图像的最终用途,但multipart/x-midex-replace内容类型的预期目的是为每个接收到的部分完全替换以前接收到的响应。把它想象成视频的帧;一次只显示一张图片,之前的图片将被丢弃 拖延几乎从来都不是万无一失的解决办法。特别是在iPhone上,你会遇到各种各样难以想象的网络情况,依靠帧间的神奇数字延迟可能在某些时候仍然会失败
因为您可以控制服务器,所以我建议删除多部分。确保在向服务器发送多个请求时,不会阻止iPhone应用程序的主线程。使用NSOperations或其他HTTP库(如)使图像获取操作异步。我使用此代码成功地实现了这一点。重要的是创建两个缓冲区来接收数据。如果您只使用一个,您将有一些双重访问问题(流访问和jpg编解码器访问)和损坏的jpg数据。 请随时向我询问更多细节
- (IBAction)startDowload:(id)sender {
NSURL *url = [NSURL URLWithString:@"http://210.236.173.198/axis-cgi/mjpg/video.cgi?resolution=320x240&fps=5"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"GET"];
/*
I create 2 NSMutableData buffers. This points is very important.
I swap them each frame.
*/
receivedData = [[NSMutableData data] retain];
receivedData2 = [[NSMutableData data] retain];
currentData = receivedData;
urlCon = [[NSURLConnection alloc] initWithRequest:req delegate:self];
noImg = YES;
frame = 0;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response
{
// this method is called when the server has determined that it
// has enough information to create the NSURLResponse
// it can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.
// receivedData is declared as a method instance elsewhere
UIImage *_i;
@try
{
_i = [UIImage imageWithData:currentData];
}
@catch (NSException * e)
{
NSLog(@"%@",[e description]);
}
@finally
{
}
CGSize _s = [_i size];
[imgView setImage:_i];
[imgView setNeedsDisplay];
[[self view] setNeedsDisplay];
}
/*
Buffers swap
*/
if (currentData == receivedData)
{
currentData = receivedData2;
}
else
{
currentData = receivedData;
}
[currendData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// append the new data to the currentData (NSData buffer)
[currendData appendData:data];
}
谢谢!我不知道multipart/x-mixed-replace的目的是什么。这解释了很多!