我正在开发一个网络摄像头查看器,对于这些摄像头:http: //www.canvision.net/support/pt300-cgi/GetData.htm
数据是这样到达的:
HTTP/1.0 200 OK
Date: Wed, 19 Feb 2003 03:40:16 GMT
Server: WYM/1.0
Connection: close
Content-Type: multipart/x-mixed-replace;boundary=WINBONDBOUDARY
Last-Modified: Wed, 19 Feb 2003 03:40:16 GMT
Pragma: no-cache
Cache-Control: no-cache
Expires: 01 Jan 1970 00:00:00 GMT
--WINBONDBOUDARY
Content-Type: image/jpeg
--WINBONDBOUDARY
Content-Type: image/jpeg
这些是我的 NSURLConnection 委托方法:
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)data
{
[currentData appendData:data];
}
- (void)connection:(NSURLConnection *)theConnection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"response with mime: %@ length:%i",[response MIMEType],[currentData length]);
if([[response MIMEType] isEqualToString:@"image/jpeg"] && [currentData length] != 0){
currentFrame = [UIImage imageWithData:currentData];
NSLog(@"valid frame");
[image setImage:currentFrame];
frameCounter++;
}
else {
NSLog(@"other data:%@",currentData);
}
currentFrame = nil;
if (currentData == receivedData1)//swap buffers
currentData = receivedData2;
else
currentData = receivedData1;
[currentData setLength:0];
[currentFrame release];
}
这一切都很好,控制台输出看起来像你期望的那样:
[Session started at 2011-02-21 20:20:39 +0100.]
2011-02-21 20:20:43.237 MotionJPEG[16330:207] response with mime: multipart/x-mixed-replace length:0
2011-02-21 20:20:43.261 MotionJPEG[16330:207] other data:<>
2011-02-21 20:20:43.262 MotionJPEG[16330:207] response with mime: image/jpeg length:0
2011-02-21 20:20:43.263 MotionJPEG[16330:207] other data:<>
2011-02-21 20:20:43.340 MotionJPEG[16330:207] response with mime: image/jpeg length:4608
2011-02-21 20:20:43.340 MotionJPEG[16330:207] valid frame
2011-02-21 20:20:43.445 MotionJPEG[16330:207] response with mime: image/jpeg length:4600
2011-02-21 20:20:43.446 MotionJPEG[16330:207] valid frame
2011-02-21 20:20:43.544 MotionJPEG[16330:207] response with mime: image/jpeg length:4580
2011-02-21 20:20:43.545 MotionJPEG[16330:207] valid frame
但是,Cam 支持另一种模式,其中附加信息嵌入在 jpeg 之间:
HTTP/1.0 200 OK
Date: Wed, 19 Feb 2003 03:40:16 GMT
Server: WYM/1.0
Connection: close
Content-Type: multipart/x-mixed-replace;boundary=WINBONDBOUDARY
Last-Modified: Wed, 19 Feb 2003 03:40:16 GMT
Pragma: no-cache
Cache-Control: no-cache
Expires: 01 Jan 1970 00:00:00 GMT
--WINBONDBOUDARY
Content-Type: image/jpeg
--WINBONDBOUDARY
Content-Type: text/plain
--WINBONDBOUDARY
Content-Type: image/jpeg
--WINBONDBOUDARY
Content-Type: text/plain
在这种模式下,我希望代码可以正常工作,并且 text/plain 部分将由
NSLog(@"other data:%@",currentData);
线。但是,由于某种原因,输出看起来完全一样,它从来没有用 mime: text/plain 表示响应,而是将纯文本部分附加到 jpeg 中,并且 UIImageView 无法显示数据。为什么 NSURLConnection 不能识别 text/plain 部分?这是完整的来源:http ://renehopf.de/MotionJPEG.zip 但要重现问题,您需要相同的网络摄像头...
谢谢,
雷内