It’s not a very common feature, but sometimes you need to create a custom layout for iPhone gallery. My team and I had to code it on a project we are currently working on. Luckily that wasn’t the first custom gallery we had to build, so I’ve found an old code and change layout a bit. It worked great, and we saved a day of development.

Creating layout we want depends on UI so in the example we’ll have the simple one. Fetching user’s albums and pictures is the real problem. We are going to create three extensions for PHPhotoLibrary, PHAssetCollection, and PHAsset. With minor changes you will reuse them anytime you need to create a custom gallery for your iOS apps.

Firstly we need to get permission to access the user’s gallery.

After getting it, we need to fetch albums. Before fetching we will select type and subtype of albums. There are three types of albums: album, smartAlbum and moment. We won’t show albums which have no assets.

When presenting an album, we need a title and cover image. Apple’s default cover picture is the last picture in a collection, so you can change that when implementing the code.

Finally, we need to get photos for the selected album. The good thing is that we can create fetch option predicate and specify what kind of media type we want.

For presenting images in cells, we will use getAssetTumbnail method. In this example, I haven’t used getOriginalImage and getImageFromPHasset methods because we are not uploading pictures. However, in the real app, these methods will be useful.

Creating custom gallery is very easy. Moreover, when you finish coding you will reuse it on all other projects. I hope that you find this useful. Here is code example.

Thanks for reading :)

If this article was helpful and you want to hear more about similar topics, please clap, share, follow or comment.