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 |
| Project style | 10 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: Sendable or @Sendable, async declaration or closure, @MainActor; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: SwiftUI state property wrapper, @Observable. |
| Key frameworks/packages | SwiftUI, CoreGraphics, Foundation, ImageIO, UIKit; these are source dependencies, not architecture labels. |
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
flowchart LR
Bundle["Sample bundle"]
V1["VisionWatchSample iOS"]
V2["VisionWatchSample watchOS"]
Boundary["SwiftUI / CoreGraphics APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
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
classDiagram
VisionWatchSampleApp *-- AnimalStore : animalStore
CroppedImageCache *-- CroppedImageCache : shared
CroppedImageCache o-- Cache : cache
ImageCrop *-- CGRect : rect
Ownership evidence
VisionWatchSample watchOS/VisionWatchSampleApp.swift:12 — stored dependency or nearest verified ownership anchor
@main
struct VisionWatchSampleApp: App {
@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.
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 |
|---|---|---|---|
| Transfer contract | Sendable or @Sendable |
The source declares a sendability boundary; this alone does not synchronize mutable state. | Shared/Vision/SaliencyImageProcessor.swift:21 |
| Suspension boundary | async declaration or closure |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | Shared/Vision/SaliencyImageProcessor.swift:55 |
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | VisionWatchSample watchOS/AnimalStore.swift:16 |
@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
Shared/Vision/SaliencyImageProcessor.swift:21 — representative execution boundary
#if os(iOS)
private final class CroppedImageCache: @unchecked Sendable {
static let shared = CroppedImageCache()
// ...
}
#endifState 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 | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | VisionWatchSample iOS/ContentView.swift:29 |
| State propagation | @Observable |
Observation macro publishes source-visible changes. | VisionWatchSample watchOS/AnimalStore.swift:17 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | Shared/Views/ImageLoadingPlaceholder.swift:8 |
| Source import | CoreGraphics |
The cited file imports this module; runtime use and architectural role are not inferred. | Shared/Vision/SaliencyImageProcessor.swift:8 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | Shared/Models/Animal.swift:8 |
| Source import | ImageIO |
The cited file imports this module; runtime use and architectural role are not inferred. | Shared/Vision/SaliencyImageProcessor.swift:9 |
| Source import | UIKit |
The cited file imports this module; runtime use and architectural role are not inferred. | Shared/Vision/SaliencyImageProcessor.swift:11 |
| Source import | Vision |
The cited file imports this module; runtime use and architectural role are not inferred. | Shared/Vision/SaliencyImageProcessor.swift:13 |
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
VisionWatchSample iOS/VisionWatchSampleApp.swift:11 — representative type boundary
@main
struct VisionWatchSampleApp: App {
// ...
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
#if os(iOS)
private final class CroppedImageCache: @unchecked Sendable {
// ...
}
#endifSwift 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. |
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
VisionWatchSampleAppis 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 |
Cited implementation, VisionWatchSampleApp |
VisionWatchSample watchOS/VisionWatchSampleApp.swift |
Cited implementation, VisionWatchSampleApp |
Shared/Vision/SaliencyImageProcessor.swift |
Cited implementation, Sendable or @Sendable, async declaration or closure, CoreGraphics, ImageIO, UIKit, Vision, CroppedImageCache, ImageCrop, SaliencyImageProcessor |
VisionWatchSample watchOS/AnimalStore.swift |
AnimalStore, @MainActor, @Observable |
VisionWatchSample iOS/ContentView.swift |
SwiftUI state property wrapper, ContentView, AnimalRow, ThumbnailView |
Shared/Views/ImageLoadingPlaceholder.swift |
SwiftUI, ImageLoadingPlaceholder |
Shared/Models/Animal.swift |
Foundation, Animal |
VisionWatchSample watchOS/AnimalDetailView.swift |
AnimalDetailView, CroppedImageView |
VisionWatchSample iOS/AnimalDetailView.swift |
AnimalDetailView |
VisionWatchSample watchOS/ContentView.swift |
ContentView |