Converting Frame Data to an Image
Defined in framework ScanditCaptureCore
When processing scanned barcodes or other data capture results, you may need to convert the frame data provided in callbacks to a platform-native image for display or storage. This guide shows how to convert FrameData to UIImage on iOS and Bitmap on Android.
Overview
The ImageBuffer class provides a convenient ImageBuffer.image property that returns a UIImage directly. This handles the YUV-to-RGB conversion internally, so you don’t need to manipulate image planes manually.
Basic Usage
Access the image from any data capture listener that provides frame data. The image must be extracted synchronously inside the callback — frame data is recycled once the callback returns:
func sparkScan(
_ sparkScan: SparkScan,
didScanIn session: SparkScanSession,
frameData: FrameData?
) {
guard let barcode = session.newlyRecognizedBarcode else { return }
// Extract the image synchronously — frame data is only valid inside the callback
let thumbnail = frameData?.imageBuffers.first?.image
DispatchQueue.main.async {
// Safe to use the UIImage on any thread after extraction
self.imageView.image = thumbnail
}
}
Note
Do not access frameData after the callback returns — extract the UIImage first.
Saving to Disk
// Inside the callback
if let image = frameData?.imageBuffers.first?.image,
let jpegData = image.jpegData(compressionQuality: 0.8) {
let fileURL = FileManager.default.temporaryDirectory
.appendingPathComponent("captured_frame.jpg")
try? jpegData.write(to: fileURL)
}
Important Notes
Frame data is only valid within the callback scope. Extract the
UIImagebefore the callback returns — the underlying pixel buffer is recycled after that.The ImageBuffer.image property performs a format conversion, which can be expensive. Cache the result if you need it more than once.
The resulting
UIImageis safe to use on any thread after extraction.Resize images to the minimum size you need to reduce memory usage.
See Also
FrameData – Interface for accessing frame data
ImageBuffer – Image buffer with conversion methods
ImagePlane – Individual image plane within a buffer