JPEG损坏的JPEG数据:标记0xdb之前的2个无关字节

时间:2014-10-21 10:27:55

标签: ios jpeg

我正在开发一个应用程序,其中我必须显示来自ip cam的视频流,我已经使用了SPMJPEG流。这是我的代码片段 -

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

@try
{

[_receivedData appendData:data];

NSRange beginRange = [_receivedData rangeOfData:_beginMarkerData
                                        options:0
                                          range:NSMakeRange(0, _receivedData.length)];

NSRange endRange = [_receivedData rangeOfData:_endMarkerData
                                      options:0
                                range:NSMakeRange(beginRange.location+beginRange.length,     _receivedData.length - (beginRange.location+beginRange.length))];


if(endRange.location != NSNotFound)
{
long endLocation = endRange.location + endRange.length;

if (_receivedData.length >= endLocation)
{
//Getting data in imageData in both iOS 7 and iOS 8
NSData *imageData = [_receivedData subdataWithRange:NSMakeRange(beginRange.location,     endLocation-beginRange.location)];

            [_receivedData replaceBytesInRange:NSMakeRange(0, endLocation) withBytes:NULL length:0];

UIImage *receivedImage = nil;
//Getting data in receivedImage in iOS 7 but not in iOS 8, returning nil image in iOS 8
receivedImage = [UIImage imageWithData:imageData];

if (receivedImage)
{
            static double movieSize = 0;
            movieSize += imageData.length;

            NSLog(@"Frame size in kb: %f, since begining: %f", imageData.length / 1024.0, movieSize / 1024.0);

            double delay = (!lastFrameDate) ? 0 : -[lastFrameDate timeIntervalSinceNow];

            lastFrameDate = [NSDate date];

            self.frame = [SPMJPEGFrame frameWithImage:receivedImage timeDelay:delay];
        }
    }
}
       }
@catch (NSException *e)
{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Warnning!" message:[e description] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];
    return;

}
}

它在iOS 7中运行得相当不错,但它根本无法在iOS 8中运行,我收到错误" ImageIO:JPEG损坏的JPEG数据:标记0xdb和#34之前的2个无关字节;但它不是iOS 7中的一个问题,但我从谷歌搜索知道iOS 8不允许在UIImageView中显示损坏的JPEG图像。(我使用UIImageView显示流URL)请帮助我相同,提前谢谢

1 个答案:

答案 0 :(得分:0)

基于类似的抱怨,IOS 8 JPEG解码器听起来比IOS 7版本更严格。我自己的理论是一些编码器在计数标记后插入额外的字节。到目前为止,没有人花时间确定:

  1. 创建这些JPEG图像的应用程序
  2. 导致问题的标记
  3. FFDB是定义量化表标记。也许你可以告诉使用什么创建JPEG文件,你可以运行某种类型的JPEG转储程序来分析流的结构。我怀疑你会发现:

       FF<Marker ID><2-byte length = L><L bytes of data><2 more bytes>FFDB 
    

    我的赌注是问题标记是一个糟糕的EXIF标题。

    与此同时,可能的解决方案是:

    一个。使用不同的JPEG编码器  湾编写一个简单的过滤器,删除JPEG流中多余的字节。