🖼️

Exploring iOS 16 Photos Picker in Swift UI

Mar 4, 2025

Introduction to iOS 16 Photos Picker in Swift UI

  • iOS 16 introduces a native Photos Picker in Swift UI.
  • This lecture demonstrates how to select single/multiple images and filter file types.
  • Example: Building a profile screen with image selection.

Setting Up Photos Picker

  • Import Libraries: Import PhotosUI under SwiftUI.
  • Setup UI: Use HStack for avatar image and VStack for name and profession on the profile screen.

Implementing Photos Picker

  • Wrapping Image: Wrap the profile image with PhotosPicker to pull up image selection on tap.
  • Label Binding: Use the label parameter to customize the button (e.g., Image, Text, SF Symbol).
  • Photos Picker Item: Bind selection to @State private var photosPickerItem: PhotosPickerItem?.
    • Converts data, not a fully rendered image, for performance.

Filtering Photos

  • Filter Options: Use PHPickerFilter to filter file types (e.g., images, videos).
  • Exclude Filter: Example shown to exclude screenshots.

Converting Picker Item to UIImage

  • OnChange Modifier: Monitor changes in photosPickerItem.
  • Conversion: Convert the selected PhotosPickerItem to UIImage and update the UI.
  • Nil Check: Ensure photosPickerItem is not nil before conversion.

Handling Multiple Images

  • Array Handling: Change photosPickerItem to array photosPickerItems.
  • Max Selection Limit: Limit selections (e.g., max 5 images).
  • Ordered Selection: Maintain the order of selection using selectionBehavior: .ordered.

Updating UI for Multiple Images

  • Scroll View: Use ScrollView for horizontal scrolling of selected images.
  • Images Array: Use @State private var images: [UIImage] = [] to store selected images.
  • ForEach Loop: Iterate over images array to display images.

Final Adjustments

  • Reset Selection: Set photosPickerItems.removeAll() to clear previous selections.
  • UI Customization: Improve UI styling and layout.

Sponsored Message

  • Squarespace: Use Squarespace to efficiently build iOS Developer portfolios or landing pages.

Conclusion

  • Successfully demonstrated implementing iOS 16 Photos Picker in Swift UI.
  • Highlighted key features: single/multiple image selection, filtering, data conversion.
  • Encouragement to use Squarespace for web development to focus on building iOS apps.