Hi all,
I am having a problem when constructing a UIImage from raw JPEG data. I transfer JPEG data through the network via UDP so some packets are expected to be lost. I want to be able to detect when the JPEG cannot be decoded properly and not display it in my UI view. If I attempt to display it I get the error:
JPEGDecompressSurface : Picture decode failed: e00002d1
Decoding failed with error code -1
and my image is displayed with some grayed parts (as expected). I do not see any exception thrown, other than the error message. Here is my code:
let image = UIImage(data: imagedata, scale:1.0)
self.imageView = UIImageView(image: image)
self.imageView.frame = self.view.frame
self.imageView.contentMode = .center
self.view.addSubview(self.imageView)The imagedata are the raw JPEG data that sometimes are incomplete due to unreliable network. It is a streaming application so I am okay with some images to be skipped. I simply want to handle whenever the JPEG decode fails and move to the next image rather than displaying a broken image.
Many thanks in advance!
Dimos
I transfer JPEG data through the network via UDP so some packets are expected to be lost. I want to be able to detect when the JPEG cannot be decoded properly and not display it in my UI view.
Do not use this approach. In addition to the problems you’re seeing, it’s deeply insecure. The JPEG decoder is meant to be able to reject corrupted JPEGs, but there’s a long history of security vulnerabilities in decoders like this, where an attacker carefully crafts malicious data to trigger a bug that allows them to hijack your app. You do not want to be encouraging this.
At a minimum you should transfer your JPEG data with a checksum so that you can detect accidental brokenness (like a dropped datagram). However, I strongly advise you to protect this data cryptographically, for example, by using DTLS.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"