A small iOS library that provides a mask-based erase/draw view over one or two images. Use your finger or a stylus to erase (reveal the bottom image) or draw (restore the top image) with undo/redo support.
- iOS 13+
- Swift 5.9+
- Xcode 15+
Add the Eraser package to your project:
- In Xcode: File → Add Package Dependencies…
- Enter the repository URL (or add a local path to this package).
- Add the Eraser library to your app target.
Or add to your Package.swift:
dependencies: [
.package(url: "https://github.com/serhatakalin/Eraser.git", from: "1.0.0")
]Typical use: put after on top of before; erase reveals the bottom image, draw restores the top image on the same paths.
- Create an
EraserViewand add it to your view hierarchy. - Set
lineWidth(brush size) andmode:.eraseor.draw. - Call
configure(foreground:afterImage, background:beforeImage). - Optionally set
onUndoRedoStateChangedto enable/disable undo/redo buttons.
import Eraser
let eraserView = EraserView(frame: view.bounds)
view.addSubview(eraserView)
eraserView.lineWidth = 24
eraserView.mode = .erase
eraserView.onUndoRedoStateChanged = { canUndo, canRedo in
undoButton.isEnabled = canUndo
redoButton.isEnabled = canRedo
}
eraserView.configure(foreground: afterImage, background: beforeImage)- Mode
.erase— strokes reveal the before (bottom) image. - Mode
.draw— strokes bring back the after (top) image on those paths.
Use configure(with: image) for a single image; erase/draw then affect that image with the view’s background showing where erased.
eraserView.configure(with: myImage)eraserView.undo()— undo last strokeeraserView.redo()— redo last undone strokeeraserView.resetToMask()— clear all strokes and reset
eraserView.changeSource(for: anotherImage)— replace the foreground (and optional background) and reinitialize.
This repository includes an EraserDemo iOS app in the Demo/ folder. Open Demo/EraserDemo.xcodeproj in Xcode (the Eraser package is linked as a local dependency), then run the EraserDemo scheme on a simulator or device. The demo uses before and after assets: the toolbar has mode (Erase / Draw), brush size, Undo, Redo, and Reset.
See the repository license file.
