Swiftly over PhotoKit (1)

Miras Karazhigitov
5 min readAug 4, 2019

--

Most of the apps nowadays need to work with photos and videos. Apple made an amazing framework that does just that — PhotoKit. It allows 3rd party apps access and manage assets (photo or video) from Photos app

‘’’Using PhotoKit, you can fetch and cache assets for display and playback, edit image and video content, or manage collections of assets such as albums, Moments, and Shared Albums.’’’

So let’s figure it out in practice. We will make a simple app to fetch all photos and videos from your camera roll and display them in a collection.

Full code: https://github.com/mirasaujan/MyMotoIsPhoto

Fetching assets

Fetching assets in Photos is extremely easy. Here is the code for it

Creating fetch result

(1) Import Photos framework

(2) Create an instance var fetchResult. We will use it to get images later

(3) fetchAssets method instantiates fetchResult.

PHAsset contains metadata about a concrete asset in the gallery. A static method fetchAssets, returns PHFetchResult

PHFetchResult is a wrapper around collection of PHAssets from fetch request.

(4) We can change order in which assets are created or filter out assets we don’t need using options (PHFetchOptions).

Example of PHFetchOption

We have a method to get assets from the gallery.

Displaying assets

Pretty standard, nothing much to explain. But there is a one little problem. Though fetch result contains information about assets it does not contain an actual image/video. 😧

It has been done this way boost fetching performace. Images are memory expensive. If we try to load a bunch of images on our iPhone, firstly, it will take forever. Secondly, iPhone will most likely crash. To solve this problem, Photos make a PHAsset instance to all photos. It contains metadata to help distinguish them and sort during fetch request. It also contains information to retrieve actual image. In order to get the image you need to feed an instance of PHAsset to PHImageManager.

PHImageManager allows to access concrete image/video with an instance of PHAsset. There is also PHCachingImageManager — that leverages caching.

Let’s implement a UICollectionViewDataSource.

(1) we get the total number of assets in fetch result.

(2) Dequeue a collection view cell — GalleryCell. All it has is an id — string. (We will need it later)

(3) We get a PHAsset that corresponds to cell’s index

(4) use image manager to request Images and set it to cells background view.

You can browse imageManager’s api here (PHImageManager). You can use it to get AVAsset if you need a video to edit, or you might prefer AVPlayerItem if you only need video for playback, but I digress.

Full code (without PHFetchOption extension)

Privacy

Now with a collection view set, lets test it out. RUN!

🤬ERROR

Apple is pretty serious about privacy. App developers need to specify that their app will want to use user’s Photo Library. To make Apple happy we need to go to Info.plist and add another row to Information Property List.

Re-run

Press YES😬

Hmm, what is it again? I see a blank screen.

Re-run

Lol, it is working!?

Bug fixes

The problem is in the first set up. After you allow an app to access the gallery it does not refresh a collection view. Try to delete an app from your simulator/device and run. You will see the same behavior. Well, we need to fix it!

PHPhotoLibrary allow to request info, changes to gallery. The method that we used makes a request to determine if user granted access to his gallery. If so, reloads collection view.

What if I told you there is a bug in this app. Have you found it already? Well, if you install this demo app on a phone with a bunch of assets and scroll really fast you will see what I am talking about.

Images get set to wrong cells, and fix themselves shortly. It does not look pleasant so let’s fix that too.

We have already made a custom cell with identifier. All we need to do is to set asset’s id to cell’s id and check before setting.

Ok, let’s finish here for now. We made an app that displays assets from the camera roll and discovered that it is not as simple as it seems. There are pit falls to look out for. In the next post, I will show you how to monitor changes in your Photos app, and add new assets to it.

Part 2, OUT SOON🙌🏻

clappButton.smash(times: 50)

Follow me on Twitter and dont miss the next post.

--

--