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
Project style 6 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: DispatchQueue.main.async, async declaration or closure, @MainActor, Task closure isolated to MainActor, Task; none alone proves a background thread.
State/event model Source-visible mechanisms: ObservableObject, @Published, SwiftUI state property wrapper.
Key frameworks/packages SwiftUI, Foundation, PhotosUI, Vision, CoreImage; these are source dependencies, not architecture labels.

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.

Concurrency, scheduling, and thread safety

Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.

Concern Source mechanism Verified placement or handoff Evidence
Queue scheduling DispatchQueue.main.async The source addresses the main dispatch queue. PersonInstanceMaskDemo/Models/PhotoSelectionModel.swift:54
Suspension boundary async declaration or closure The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. PersonInstanceMaskDemo/Models/SegmentationModel.swift:30
Main isolation @MainActor The cited annotation marks its attached declaration or closure as main-actor isolated. PersonInstanceMaskDemo/Models/SegmentationModel.swift:60
Main isolation Task closure isolated to MainActor The cited operation explicitly enters a main-actor-isolated region. PersonInstanceMaskDemo/Models/SegmentationModel.swift:60
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. PersonInstanceMaskDemo/Models/SegmentationModel.swift:60

@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.

Reference code

PersonInstanceMaskDemo/Models/PhotoSelectionModel.swift:54 — representative execution boundary

            DispatchQueue.main.async {
                // ...
                    print("Failed to get the selected item.")
                // ...
            }

State propagation, frameworks, and dependencies

Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.

Category Mechanism or module Verified role Evidence
State propagation ObservableObject ObservableObject supplies an observation contract. PersonInstanceMaskDemo/Models/PhotoSelectionModel.swift:13
State propagation @Published A published property can emit owner-controlled changes. PersonInstanceMaskDemo/Models/PhotoSelectionModel.swift:40
State propagation SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. PersonInstanceMaskDemo/Views/PhotoSelectionView.swift:12
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. PersonInstanceMaskDemo/Models/PhotoSelectionModel.swift:11
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. PersonInstanceMaskDemo/Models/PhotoSelectionModel.swift:8
Source import PhotosUI The cited file imports this module; runtime use and architectural role are not inferred. PersonInstanceMaskDemo/Models/PhotoSelectionModel.swift:9
Source import Vision The cited file imports this module; runtime use and architectural role are not inferred. PersonInstanceMaskDemo/Models/SegmentationModel.swift:12
Source import CoreImage The cited file imports this module; runtime use and architectural role are not inferred. PersonInstanceMaskDemo/Models/SegmentationModel.swift:8

receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.

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

class PhotoSelectionModel: ObservableObject {
    // ...
    private(set) var imageState: ImageState = .noneselected
    // ...
}

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.

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 Cited implementation, PersonInstanceMaskDemoApp
PersonInstanceMaskDemo/Models/PhotoSelectionModel.swift Cited implementation, DispatchQueue.main.async, ObservableObject, @Published, SwiftUI, Foundation, PhotosUI, PhotoSelectionModel, ImageState, TransferError, SelectedImage
PersonInstanceMaskDemo/Models/SegmentationResults.swift SegmentationResults, Cited implementation, InstanceMaskResults, PersonSegmentationResults
PersonInstanceMaskDemo/Models/SegmentationModel.swift Cited implementation, async declaration or closure, @MainActor, Task closure isolated to MainActor, Task, Vision, CoreImage, SegmentationModel, RequestState
PersonInstanceMaskDemo/Views/PhotoSelectionView.swift SwiftUI state property wrapper, PhotoSelectionView, SelectedImageView
PersonInstanceMaskDemo/Views/SegmentationResultsView.swift SegmentationResultsView, SegmentationMaskView