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

Implementing saliency-based image cropping in iOS and watchOS

At a glance

Item Summary
Purpose Crop regions most likely drawing people’s attention from an image in your iOS or watchOS app.
App architecture A Swift sample bundle with entry-bearing project variants VisionWatchSample iOS, VisionWatchSample watchOS, each leading to SwiftUI / CoreGraphics APIs.
Main patterns Central store, Actor-isolated state
Project style 10 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.

Project structure

Source bundle/
├── VisionWatchSample iOS/
│   ├── VisionWatchSampleApp.swift
│   ├── ContentView.swift
│   └── AnimalDetailView.swift
├── VisionWatchSample watchOS/
│   ├── VisionWatchSampleApp.swift
│   ├── AnimalDetailView.swift
│   ├── AnimalStore.swift
│   └── ContentView.swift
├── Shared/
│   ├── Vision/
│   │   └── SaliencyImageProcessor.swift
│   ├── Models/
│   │   └── Animal.swift
│   └── Views/
│       └── ImageLoadingPlaceholder.swift
├── Configuration/
│   └── SampleCode.xcconfig
└── VisionWatchSample.xcodeproj/
    └── .xcodesamplecode.plist

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 3 project/configuration file(s) and 15 source declaration(s).

Overall architecture

Reference code

VisionWatchSample iOS/VisionWatchSampleApp.swift:10 — architecture anchor

@main
struct VisionWatchSampleApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Interpretation

The branches represent separate entry-bearing project variants in the downloaded bundle, not runtime calls between those variants. Each branch is intentionally collapsed at the documented framework boundary; the detailed target-local flow remains in the cited files. Ownership is claimed only where the next section cites a stored property or assignment.

Ownership and state

Ownership evidence

VisionWatchSample watchOS/VisionWatchSampleApp.swift:12 — stored dependency or nearest verified ownership anchor

    @State private var animalStore = AnimalStore()
Owner Object or state Relationship Mutation authority
VisionWatchSampleApp AnimalStore (animalStore) owns wrapper-managed state Owning lexical scope
CroppedImageCache CroppedImageCache (shared) creates and retains Initialized by the owner; the binding is immutable
CroppedImageCache Cache (cache) stores or receives Initialized by the owner; the binding is immutable
ImageCrop CGRect (rect) owns value state Initialized by the owner; the binding is immutable

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

VisionWatchSample iOS/VisionWatchSampleApp.swift:11 — representative type boundary

struct VisionWatchSampleApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
Type Responsibility Depends on or conforms to
VisionWatchSampleApp Application entry and top-level composition App
VisionWatchSampleApp Application entry and top-level composition App
SaliencyImageProcessor Owns a feature processing stage Concrete collaborators/imported frameworks
ContentView User-interface presentation and input forwarding View
ThumbnailView User-interface presentation and input forwarding View
AnimalDetailView User-interface presentation and input forwarding View
CroppedImageView User-interface presentation and input forwarding View
AnimalDetailView User-interface presentation and input forwarding View
AnimalStore Centralized state or persistence access Concrete collaborators/imported frameworks
ContentView User-interface presentation and input forwarding View

No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.

Access control

Symbol Access Verified effect Likely rationale
cache (Shared/Vision/SaliencyImageProcessor.swift:23) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.
salientPadding (Shared/Vision/SaliencyImageProcessor.swift:51) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.
imageSize (Shared/Vision/SaliencyImageProcessor.swift:55) 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.
salientBoundingBox (Shared/Vision/SaliencyImageProcessor.swift:69) 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

Shared/Vision/SaliencyImageProcessor.swift:23 — representative boundary

    private let cache = NSCache<NSString, UIImage>()

    /// Returns the cached cropped image for `key`, or `nil` if not yet stored.
    func image(for key: String) -> UIImage? {
        cache.object(forKey: key as NSString)
    }

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 VisionWatchSampleApp The source’s App suffix makes this role explicit.
Owns a feature processing stage SaliencyImageProcessor The source’s Processor suffix makes this role explicit.
Centralized state or persistence access AnimalStore The source’s Store suffix makes this role explicit.
User-interface presentation and input forwarding AnimalDetailView, ContentView, CroppedImageView, ThumbnailView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Central store VisionWatchSample watchOS/AnimalStore.swift:17 A store-named type centralizes feature state or persistence.
Actor-isolated state VisionWatchSample watchOS/AnimalStore.swift:16 Actor annotations make the concurrency ownership boundary explicit.

Naming conventions

  • Types: App: VisionWatchSampleApp; Processor: SaliencyImageProcessor; Store: AnimalStore; View: AnimalDetailView, ContentView, CroppedImageView, ThumbnailView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: image, store, imageSize, salientBoundingBox, generateThumbnail, crop, pad, squareCropRect.
  • Files: VisionWatchSample iOS/VisionWatchSampleApp.swift, VisionWatchSample watchOS/VisionWatchSampleApp.swift, Shared/Vision/SaliencyImageProcessor.swift, VisionWatchSample iOS/ContentView.swift, VisionWatchSample watchOS/AnimalDetailView.swift, VisionWatchSample iOS/AnimalDetailView.swift.

Architecture takeaways

  • VisionWatchSampleApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, CoreGraphics, ImageIO, UIKit 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.
  • The source does not justify labeling the design protocol-oriented.

Source map

Source file Relevant symbols
VisionWatchSample iOS/VisionWatchSampleApp.swift VisionWatchSampleApp
VisionWatchSample watchOS/VisionWatchSampleApp.swift VisionWatchSampleApp
Shared/Vision/SaliencyImageProcessor.swift CroppedImageCache, ImageCrop, SaliencyImageProcessor
VisionWatchSample iOS/ContentView.swift ContentView, AnimalRow, ThumbnailView
VisionWatchSample watchOS/AnimalDetailView.swift AnimalDetailView, CroppedImageView
VisionWatchSample iOS/AnimalDetailView.swift AnimalDetailView
VisionWatchSample watchOS/AnimalStore.swift AnimalStore
VisionWatchSample watchOS/ContentView.swift ContentView
Shared/Models/Animal.swift Animal
Shared/Views/ImageLoadingPlaceholder.swift ImageLoadingPlaceholder