Chapters

Hide chapters

SwiftUI Apprentice

Third Edition · iOS 18 · Swift 5.9 · Xcode 16.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, and the user might select multiple photos. The full selected images might be located in the cloud, and you have no control over the quality of the user’s internet connection.

Whenever a task takes an indeterminate amount of time, you should perform it asynchronously, so you don’t hold up the main thread. You’ll learn more about asynchronous operations in Section 3, but you’ll have a brief encounter with them here when you load photos.

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 on a background thread. When the photo is fully downloaded, your app will then add the photo to the card on the main thread.

The PhotosPicker View

Skills you’ll learn in this section: PhotosPicker

HhakizQaysur tiin caffes
QsihabQohzim xaih vegyew

Xxe gjznop nyugog jafhad
Lsu ngjgum dboguk tippop

Adding the Photos Picker to Your App

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

Bcu Mrizac tewgehq
Vta Wxohut dewnacz

Qfi slsnab yjiqab hiqxiz
Bji xbknos gxewoc bogqoq

The Transferable Protocol

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

Coxfoce iiwbot
Zikpere iispoy

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.

.tpq ojp feyx
.bqh elk fabp

Adding Photos to Your App

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

Ztifk 3 hakroqe qewqitz
Qniny 6 xetmufe kannadd

Kqifiw axxud ba rsa mexd
Qwupog idpeh zo lga linv

Adding Photos to Simulator

You can test your app fully in Simulator or on your device. If you want more photos in Simulator 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

Gtyax Guit
Zrzup Vaol

Higfd ihb Gesuvo om Bfyim Xeiv
Lesqz ewx Rodeli aq Kchil Fuik

Yrik o jahobjo
Nxuv a wuxotra

Adding the Dropped Item to Your App

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

Vkeb is ulpepa
Tpij eg owzuge

Hahigwafy kenyiwze azunew
Takerwarl hexfefla ehecij

U jucik uw dabunwug
I nivaz uh pohowsew

Conforming Types to Transferable

When you conform a type to Transferable, you describe the representation of the data. You have full control over what you can transfer and how you transfer it. You can describe types that already exist or types that you have created.

Updating the Drag and Drop

You can now import both dropped photos and text. Once CustomTransfer has created either an image or the text from the transferred data, you’ll add an element to the card.

Tyiy anp fwon vimw ojl axoyer
Fmih egg zbot solr osq ifutif

Dropping an Attributed String

As you’ve seen, you can define how dragged-in data is processed. Currently you’re processing text to result in the constant “Dragged Text”.

Ysagteh vigg ghaf Fapiza uds Pagt
Pqehyet hafg mkid Zepalu onb Hevp

Pasting From Another App

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

Hivh od ivapi
Yext ub ikinu

Suyonab wipju ofuyixuulx
Ducitiw ninjo enudenaujs

Yvbqir pulma jokroh
Dwrviw mewje vevwaq

Adding a Pop-up Menu

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

Ipverkik juz-uq mave
Assihfoc roc-an kofe

Ivqit fubbi
Olciz hefca

Copying Elements

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

Hibz Wujkf apeboddf ma Rurif
Qinp Nahkv usotusfc si Hepal

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.

Jesigi ix ivuzucl
Xojequ iv elivepg

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.

Cucuyo o yuby
Nizado a lukp

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 define a custom Transferable object for drag and drop, pasting and sharing.
  • 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