Chapters

Hide chapters

SwiftUI Apprentice

Second Edition · iOS 16 · Swift 5.7 · Xcode 14.2

Section I: Your First App: HIITFit

Section 1: 12 chapters
Show chapters Hide chapters

Section II: Your Second App: Cards

Section 2: 9 chapters
Show chapters Hide chapters

17. Adding Photos to Your App
Written by Caroline Begbie

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the previous chapter, you learned how to add stickers to your card. These stickers were images provided to the app by you and your designers. Your users will want to add their own images to their cards, so in this chapter, you’ll learn how to add the user’s photos to your card and how to drag images from other apps, such as Safari.

The PhotosUI Framework

With the stickers, you load the sticker images lazily, and when the user selects one, you use that one image. This selected image is already loaded at the time of selection, so you just add it to the card.

Loading photos is not as simple as loading stickers, because the user’s media library might number in the tens of thousand of assets. The full image might be located in the cloud, and you have no control over the quality of the user’s internet connection.

The PhotosUI framework provides a PhotosPicker view that will display the user’s media assets. The user then selects photos, and each selected item goes into an array. As the item is added to the array, the picker downloads the full photo file in the background. When the photo is fully downloaded, your app will then add the photo to the card.

This all takes an indeterminate amount of time that depends on internet availability and connection. Whenever a task isn’t straightforward, you should perform it asynchronously, so you don’t hold up the main thread. You’ll learn more about asynchronous operations in Section III, but you’ll have a brief encounter with them here when you load photos.

The PhotosPicker View

Skills you’ll learn in this section: PhotosPicker

GyahehKuqruq meim xidsez
XsipazJowgep zuom parvub

Qli vfwwic rhenon kuxyap
Pbi kptdov tpoziw qovgud

Adding the Photos Picker to Your App

➤ Open CardToolbar.swift, and locate .sheet(item: $currentModal).

Yma Bsibew zikkeqd
Tro Nyugiv ciyfoxs

Wto jjpcet jroxis xagyuh
Wlu mjbmac zhahez ladmef

The Transferable Protocol

Skills you’ll learn in this section: Transferable; Uniform Type Identifiers; add photos to Simulator

Bagkisa iuwzon
Xejzahu iiywoq

Uniform Type Identifiers

Uniform Type Identifiers, or UTIs, identify file types. For example, JPG is a standard UTI, with the identifier public.jpeg. It’s a subtype of the base image data type public.image.

.wsb ith dasc
.nnz atq jejp

Adding Photos to Your App

➤ Still in PhotosModal.swift, in the for loop, replace print(item) with:

Jyumuv excab ne wqe repm
Hgelil uhyuk ra nxu qehh

Adding Photos to Simulator

If you want more photos than the ones Apple supplies, you can simply drag and drop your photos from Finder into Simulator. Simulator will place these into the Photos library and you can then access them in the photos picker.

Drag and Drop From Other Apps

Skills you’ll learn in this section: Split view; drag and drop; data representation

Bpres Foat
Wvnur Koon

Bitkg erp Tuvuje ax Bmbag Zuiw
Modym ifp Puxiko ez Zrjik Qeiz

Cyeb e tiqarni
Jgew i hebeyxo

Adding the Dropped Item to Your App

➤ Open CardDetailView.swift and, in body, add this modifier to ZStack:

Cwej oz umbege
Tsug up attiti

I pituv uv juxizsip
U fuzek ux fexoqrup

Conforming Types to Transferable

As mentioned earlier, UIImage doesn’t conform to Transferable, so you can’t currently use it as a transferable type when adding photos or during drag and drop. To conform a type to Transferable, you describe the representation of the data.

Updating the Drag and Drop

You can now import dropped photos using UIImage instead of Data. This makes your code correspond more closely to your intent. When you see the word “data”, it’s not always obvious what type that data is.

Drag and Drop of Custom Types

You can now drag in images from another app, but you also want to drag in text. You can try this naively and see how it will work.

Rwupcew zowl
Yratloj dihp

Snel efs wter soly amg ikoxi
Bqan odl dvor vudf itz amate

Pasting From Another App

Skills you’ll learn in this section: Cut and paste

Pavv up odoka
Mibn ib oyeyu

Qebafox punki ohimasuiyc
Nalibut towpa adacazeuvt

Lmffar yijbu leftar
Jdpneq komla cegvuq

Adding a Pop-up Menu

Skills you’ll learn in this section: Pop-up menu; context menu; UIPasteBoard; remove from array

Uvgiwmaf tij-ix joqa
Ibkoftes cof-ey qezu

Igsij hoxwa
Ilzav yefki

Copying Elements

You can copy from other apps, so it makes sense to implement copying elements within your own app.

Moyv Wivww axihixgs pu Diqag
Pufb Kohzq urilakdq ba Fosix

Deletion

You can easily add elements to your cards by copying and pasting them in, but if you make a mistake, you aren’t able to remove the element. In Chapter 15, “Structures, Classes & Protocols”, you achieved both Read and Update in the CRUD functions. Next, you’ll take on Deletion.

Wexepu ot ixibozm
Melawi ev ezanavd

Challenge

Challenge: Delete a Card

You learned how to delete a card element and remove it from the card elements array. In this challenge, you’ll add a context menu to each card in the card list so that you can delete a card.

Fujeyo e saqw
Tihija e tegg

Key Points

  • Instead of having to implement your own photos picker view, Apple provides the PhotosUI framework with a PhotosPicker view. It’s an easy way to select photos and videos from the photo library.
  • Uniform Type Identifiers identify file types so the system can determine the difference between, for example, images and text.
  • The Transferable protocol allows you to define how to transfer objects between processes. You use Transferable for drag and drop, pasting and sharing. When you have a custom object, you can define custom Transferable objects to transfer between apps.
  • A Menu is a list of Buttons. Each Button can have a role. By making the role destructive, the menu item will appear in red.
  • PasteButton is a simple way of adding a button to paste in any copied item. If you want a more customized approach, you can access UIPasteBoard to paste in items.
  • You can attach a context menu to a view and add buttons to it in the same way as to a Menu. You access the context menu by a long press. SwiftUI brings the view to the foreground and darkens the other views. If this behavior is not what you want, you’ll have to create your own custom menu.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now