Sample CodeiOS, iPadOS, Mac CatalystReviewed 2026-07-21View on Apple Developer

Segmenting and colorizing individuals from a surrounding scene

At a glance

Item Summary
Purpose Use the Vision framework to isolate and apply colors to people in an image.
App architecture A Swift sample with the source-visible chain PersonInstanceMaskDemoAppPhotoSelectionViewPhotoSelectionModelVision APIs.
Main patterns Protocol-oriented abstraction, Publisher-backed observable state, Actor-isolated state
Project style 6 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.

Project structure

Source bundle/
├── PersonInstanceMaskDemo/
│   ├── PersonInstanceMaskDemoApp.swift
│   ├── Models/
│   │   ├── PhotoSelectionModel.swift
│   │   ├── SegmentationModel.swift
│   │   └── SegmentationResults.swift
│   ├── Views/
│   │   ├── PhotoSelectionView.swift
│   │   └── SegmentationResultsView.swift
│   └── Info.plist
├── Configuration/
│   └── SampleCode.xcconfig
└── PersonInstanceMaskDemo.xcodeproj/
    ├── .xcodesamplecode.plist
    └── project.pbxproj

Structure observations

  • Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
  • Primary languages: Swift.
  • The verified tree contains 4 project/configuration file(s) and 14 source declaration(s).

Overall architecture

Reference code

PersonInstanceMaskDemo/PersonInstanceMaskDemoApp.swift:9 — architecture anchor

@main
struct PersonInstanceMaskDemoApp: App {
    var body: some Scene {
        WindowGroup {
            PhotoSelectionView()
        }
    }
}

Interpretation

The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into Vision.

Ownership and state

Ownership evidence

PersonInstanceMaskDemo/Models/PhotoSelectionModel.swift:27 — stored dependency or nearest verified ownership anchor

    struct SelectedImage: Transferable {
        let image: UIImage
        // ...
    }
Owner Object or state Relationship Mutation authority
SelectedImage UIImage (image) stores or receives Initialized by the owner; the binding is immutable
SelectedImage ImageState (imageState) stores or receives Owning type writes; wider scope can read
SelectedImage PhotosPickerItem (imageSelection) stores or receives App/module collaborators
SegmentationModel UIImage (segmentedImage) stores or receives App/module collaborators

Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.

Class and protocol design

PersonInstanceMaskDemo/Models/SegmentationResults.swift:12 — representative type boundary

protocol SegmentationResults {
    var segmentationMask: CVPixelBuffer { get set }
    var numSegments: Int { get set }
    func generateSegmentedImage(baseImage: CIImage, selectedSegments: IndexSet) async -> UIImage
    func segmentForPixelValue(_ value: UInt8) -> Int
}
Type Responsibility Depends on or conforms to
PersonInstanceMaskDemoApp Application entry and top-level composition App
PhotoSelectionModel Feature data or observable state ObservableObject
SegmentationModel Feature data or observable state ObservableObject
PhotoSelectionView User-interface presentation and input forwarding View
SelectedImageView User-interface presentation and input forwarding View
SegmentationResultsView User-interface presentation and input forwarding View
SegmentationMaskView User-interface presentation and input forwarding View
SegmentationResults Defines a capability or collaboration contract Concrete collaborators/imported frameworks
ImageState Represents mutable feature state Concrete collaborators/imported frameworks
TransferError Represents feature failure conditions Error

The source explicitly defines local protocol relationships: InstanceMaskResultsSegmentationResults, PersonSegmentationResultsSegmentationResults.

Access control

Symbol Access Verified effect Likely rationale
imageState (PersonInstanceMaskDemo/Models/PhotoSelectionModel.swift:39) private(set) Read access follows the declaration; writes remain in the private scope. Inference: allow observation while reserving invariant-changing writes for the owner.
loadTransferable (PersonInstanceMaskDemo/Models/PhotoSelectionModel.swift:52) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: hide an implementation step that is not part of the collaboration surface.
countFaces (PersonInstanceMaskDemo/Models/SegmentationModel.swift:100) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: hide an implementation step that is not part of the collaboration surface.
segmentAtLocation (PersonInstanceMaskDemo/Models/SegmentationModel.swift:116) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: hide an implementation step that is not part of the collaboration surface.

Reference code

PersonInstanceMaskDemo/Models/PhotoSelectionModel.swift:39 — representative boundary

    private(set) var imageState: ImageState = .noneselected
    @Published var image: CIImage? = nil
    @Published var imageSelection: PhotosPickerItem? = nil {
        // ...
    }

Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.

Logic ownership and placement

Logic Owning type or file Placement rationale
Application entry and top-level composition PersonInstanceMaskDemoApp The source’s App suffix makes this role explicit.
Feature data or observable state PhotoSelectionModel, SegmentationModel The source’s Model suffix makes this role explicit.
User-interface presentation and input forwarding PhotoSelectionView, SegmentationMaskView, SegmentationResultsView, SelectedImageView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Protocol-oriented abstraction PersonInstanceMaskDemo/Models/SegmentationResults.swift:21 A local protocol and concrete conformance create an explicit capability boundary.
Publisher-backed observable state PersonInstanceMaskDemo/Models/PhotoSelectionModel.swift:40 Published properties notify observers while mutation remains with the state object.
Actor-isolated state PersonInstanceMaskDemo/Models/SegmentationModel.swift:60 Actor annotations make the concurrency ownership boundary explicit.

Naming conventions

  • Types: App: PersonInstanceMaskDemoApp; Model: PhotoSelectionModel, SegmentationModel; View: PhotoSelectionView, SegmentationMaskView, SegmentationResultsView, SelectedImageView.
  • Protocols: SegmentationResults.
  • Methods: loadTransferable, runSegmentationRequestOnImage, toggleSegment, toggleSegmentAtLocation, isSelected, countFaces, segmentAtLocation, generateSegmentedImage.
  • Files: PersonInstanceMaskDemo/PersonInstanceMaskDemoApp.swift, PersonInstanceMaskDemo/Models/PhotoSelectionModel.swift, PersonInstanceMaskDemo/Models/SegmentationModel.swift, PersonInstanceMaskDemo/Views/PhotoSelectionView.swift, PersonInstanceMaskDemo/Views/SegmentationResultsView.swift, PersonInstanceMaskDemo/Models/SegmentationResults.swift.

Architecture takeaways

  • PersonInstanceMaskDemoApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, PhotosUI, Vision, CoreImage through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
  • Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
  • Access-control conclusions separate verified language visibility from the likely design rationale.
  • Local protocol relationships provide an explicit substitution boundary.

Source map

Source file Relevant symbols
PersonInstanceMaskDemo/PersonInstanceMaskDemoApp.swift PersonInstanceMaskDemoApp
PersonInstanceMaskDemo/Models/PhotoSelectionModel.swift PhotoSelectionModel, ImageState, TransferError, SelectedImage
PersonInstanceMaskDemo/Models/SegmentationModel.swift SegmentationModel, RequestState
PersonInstanceMaskDemo/Views/PhotoSelectionView.swift PhotoSelectionView, SelectedImageView
PersonInstanceMaskDemo/Views/SegmentationResultsView.swift SegmentationResultsView, SegmentationMaskView
PersonInstanceMaskDemo/Models/SegmentationResults.swift SegmentationResults, InstanceMaskResults, PersonSegmentationResults