Cleaning up documents with the iOS Document Scanner
Document Cleanup lets the end user erase unwanted artifacts from a scanned page while preserving the underlying document content. Artifacts include handwriting, stamps, stains, fingers, shadows, and other distracting elements. The user paints over the regions to remove. The SDK then reconstructs a clean result image, with optional text preservation.
The Scanbot SDK ships a ready-to-use SwiftUI view, SBSDKDocumentCleanupCanvas, that wraps the custom document cleanup API and handles the painting and pinch-to-zoom gestures. It's driven by an SBSDKDocumentCleanupCanvasViewModel, which owns the cleanup state, the undo / redo history, and the stroke settings. The host application stays in full control of the surrounding chrome (top bar, toolbar, buttons, sliders, etc.).
Creating the cleanup canvas
Set up the canvas as follows:
- Create an
SBSDKImageReffrom the page image. - Configure the underlying engine with an
SBSDKDocumentCleanupConfiguration. - Use both to create the view model.
- Present the SwiftUI view as usual, e.g. inside a
UIHostingController.
- UIKit
- SwiftUI
loading...
loading...
Usage of the Document Cleanup canvas
The view model publishes the current image, the processing state, and the undo / redo availability. Observe it to enable or disable your own controls and to show a progress indicator while a cleanup operation is running. To apply a cleanup action, call undo(), redo(), or reset() on it.
- UIKit
- SwiftUI
loading...
loading...
Configuration options
Configure the cleanup behavior of the underlying engine through DocumentCleanupConfiguration. In the Ready-to-Use UI, pass it as screens.cleanup.engineConfiguration.
| Property | Type | Default | Description |
|---|---|---|---|
keepText | Boolean | true | If true, the SDK keeps detected text intact while the user erases around it. Text detection runs once up front and can take a moment, so construct the cleanup pipeline asynchronously to keep the UI responsive. |
maxUndoRedoStackSize | Int | 10 | How many undo and redo steps the SDK keeps in memory. Use at least 1 so that reset can always restore the original image. When the limit is reached, the SDK merges the two oldest steps into one. The user can still return to the original image, but with fewer steps in between. |
maxCleanupResolution | Int | 0 | Caps the brushed area the SDK processes in one cleanup step, as width × height in pixels. Larger areas are processed at a lower resolution and scaled back up, which saves memory but can soften the edges near the stroke. 0 means no limit. For example, 1200000 keeps peak memory at about 0.5 GB for a 12 MP image. Prefer cleaning smaller areas over relying on this limit. |
Cleanup status
A cleanup run reports a DocumentCleanupStatus alongside the resulting image:
| Value | Meaning |
|---|---|
OK | Cleanup completed at full resolution. |
OK_BUT_REDUCED_QUALITY | Cleanup completed, but maxCleanupResolution forced an internal downscale of the cleanup region, so quality near the cleanup boundary may be reduced. If the result is not good enough, undo the last operation and apply cleanup on a smaller area. |
View model properties and callbacks
In addition to the SBSDKDocumentCleanupConfiguration, SBSDKDocumentCleanupCanvasViewModel exposes the following properties and callbacks to customize appearance and interaction:
strokeSize— diameter of the drawing brush, in view points.strokeColor— color of the stroke preview drawn while the user paints.isProcessing—truewhile a heavy cleanup operation is running; use it to drive a progress indicator and to disable your controls.canUndo/canRedo— whether an undo or redo step is currently available.undo()/redo()/reset()— apply the corresponding cleanup action.currentImageRefSnapshot()— returns the current result image as anSBSDKImageRef, e.g. to store or share it.onFailure— called when a cleanup operation fails.onReducedQuality— called when a cleanup run completes with reduced quality because the affected area exceedsmaxCleanupResolution.onAreaTooLarge— called when a stroke is rejected because the masked area is too large to process.
Want to scan longer than one minute?
Generate a free trial license to test the Scanbot SDK thoroughly.
Get free trial licenseScanbot SDK is part of the Apryse SDK product family
A mobile scan is just the start. With Apryse SDKs, you can expand mobile workflows into full cross‑platform document processing. Whether you need to edit PDFs, add secure digital signatures, or use a fast, customizable document viewer and editor, Apryse gives you the tools to build powerful features quickly.
Learn more
