在iOS 10上从JPG原始数据创建UIImage时获取“JPEGDecompressSurface:Picture decode failed:”

时间:2022-01-04 04:26:46

Since update to iOS 10, I'm getting the following error when creating UIImage from NSMutableData:

自从更新到iOS 10以来,从NSMutableData创建UIImage时出现以下错误:

JPEGDecompressSurface : Picture decode failed:e000....."

JPEGDecompressSurface:图片解码失败:e000 ......“

There is no strange or not normal behaviour resulting, but when I debug my application I see the error quite each time I create the Image.

没有产生奇怪或不正常的行为,但是当我调试我的应用程序时,每次创建Image时都会看到错误。

Here is the code, where I create the image from downloaded data using GCDAsynchSocket class:

这是代码,我使用GCDAsynchSocket类从下载的数据创建图像:

NSData *imgDataToGen = [NSData dataWithData:imgBuffer];
UIImage *img = [[UIImage alloc] initWithData:imgDataToGen];
[_delegate client:self didReceiveImage:img];

The buffer imgBuffer is a NSMutableData-Object, which holds the downloaded image data. When the download is complete the data is converted into a picture and passed to the main GUI by firing the custom delegate method. In the main GUI then the image is set in image View. Once an image is processed, the buffer is cleared.

缓冲区imgBuffer是一个NSMutableData-Object,它保存下载的图像数据。下载完成后,数据将转换为图片并通过触发自定义委托方法传递到主GUI。在主GUI中,然后在图像View中设置图像。处理完图像后,清除缓冲区。

I tried different things, like forcing the decoding of the UIImage already in background thread, but I get always same error. The image is rendered always in right way, so I really don't understand the error message.

我尝试了不同的东西,比如在后台线程中强制解码UIImage,但我总是得到同样的错误。图像总是以正确的方式呈现,所以我真的不明白错误信息。

I tried on iPhone 5c with iOS 10.0.2 and on Simulator 5s with iOS 9.2 and 10.0.2.

我尝试使用iOS 10.0.2的iPhone 5c和iOS 9.2和10.0.2的模拟器5s。

On iPhone 5c I get the error, on simulator not.

在iPhone 5c上我得到错误,在模拟器上没有。

How can I fix this error, or can this type of error be ignored?

如何修复此错误,或者可以忽略此类错误?

1 个解决方案

#1


0  

I had the same problem and solved. In my case, I didn't notice but the conversion from Data to UIImage and the saving was performed in the main thread, once I dispatched it async to the global queue with background QOS priority and only the refresh of the GUI UIImage I dispatched back to main queue, the error stop showing in the debugger. I hope it helps you.

我有同样的问题并解决了。在我的情况下,我没有注意到,但是从主要线程中执行了从Data到UIImage的转换以及保存,一旦我将其async发送到具有后台QOS优先级的全局队列,并且只返回了我发送回的GUI UIImage的刷新到主队列,错误停止在调试器中显示。我希望它对你有所帮助。

Example:

fileprivate func downloadFromURL(urlString:String, callback:((_ image:UIImage?, _ error: Error?)->Void)?)   {
    var image: UIImage?
    guard let url = URL(string: urlString) else {
        callback!(nil, iError.RuntimeError(reason: "Wrong URL for image".localized))
        return
    }
    DispatchQueue.global(qos: .background).async {
        do {
            let data = try Data(contentsOf: url)
            image = UIImage(data: data)
            DispatchQueue.main.async {
                callback!(image,nil)
                return
            }

        } catch {
            print(error.localizedDescription)
            callback!(nil, iError.RuntimeError(reason: "Data error for image".localized))
            return

        }
    }
}

#1


0  

I had the same problem and solved. In my case, I didn't notice but the conversion from Data to UIImage and the saving was performed in the main thread, once I dispatched it async to the global queue with background QOS priority and only the refresh of the GUI UIImage I dispatched back to main queue, the error stop showing in the debugger. I hope it helps you.

我有同样的问题并解决了。在我的情况下,我没有注意到,但是从主要线程中执行了从Data到UIImage的转换以及保存,一旦我将其async发送到具有后台QOS优先级的全局队列,并且只返回了我发送回的GUI UIImage的刷新到主队列,错误停止在调试器中显示。我希望它对你有所帮助。

Example:

fileprivate func downloadFromURL(urlString:String, callback:((_ image:UIImage?, _ error: Error?)->Void)?)   {
    var image: UIImage?
    guard let url = URL(string: urlString) else {
        callback!(nil, iError.RuntimeError(reason: "Wrong URL for image".localized))
        return
    }
    DispatchQueue.global(qos: .background).async {
        do {
            let data = try Data(contentsOf: url)
            image = UIImage(data: data)
            DispatchQueue.main.async {
                callback!(image,nil)
                return
            }

        } catch {
            print(error.localizedDescription)
            callback!(nil, iError.RuntimeError(reason: "Data error for image".localized))
            return

        }
    }
}