A SwiftUI dynamic property wrapper for fetching media from your photo library. (iOS, tvOS, macOS)

Overview

ios tv mac

Media

Also available as a part of my SwiftUI+ Collection – just add it to Xcode 13+

A package for simplifying the user of the camera and the user's photo library in SwiftUI.

Features

  • Dynamic property wrappers for fetching media from the photo library
  • Camera modifier for easily taking a photo
  • ImagePicker modifier for allowing the user to pick a photo from their library

Note: Permission requests and Info.plist values are not handled by this framework. You should use the standard UI to perform these actions as usual.

Example

Fetch all asset collections of a given type and subtype:

// Its not necessary to `import Photos` as the framework does this for you.
import Media

struct AlbumsView: View {

    @FetchAssetCollection(album: .album, kind: .albumRegular)
    private var albums

    var body: some View {
        List(albums) { album in
            Text(album.localizedTitle ?? "")
        }
    }

}

For camera or image picker implementations:

@State private var takePhoto: Bool = false
@State private var chooseFromLibrary: Bool = false

var body: some View {
    VStack(spacing: 20) {
        Button {
            takePhoto = true
        } label: {
            Text("Take a photo")
        }
        .camera(isPresented: $takePhoto) { result in
            print(try! result.get())
        }

        Button {
            chooseFromLibrary = true
        } label: {
            Text("Choose from Library")
        }
        .imagePicker(isPresented: $chooseFromLibrary) { result in
            print(try! result.get())
        }
    }
}

Inspiration

The property wrapper has heavily inspired by the new FetchRequest provided by SwiftUI. As such, it conforms to RandomAccessCollection allowing it to be used in all the same places, e.g. List, ForEach, etc...

This greatly simplifies the use of the photos framework library when integrating into a SwiftUI package.

Since the library is also a dynamic property, updates occuring in the users library are automatically tracked and reflecting in your SwiftUI views.

Custom Queries

The property wrapper initializers provide various overrides to ensure you can customize the queries you need to perform.

In cases where a convenience initializer hasn't been provided, you can simply pass your own PHFetchOptions instance:

private static var options: PHFetchOptions = {
    let options = PHFetchOptions()
    options.predicate = ...
    return options
}()

@FetchAssetCollection(options)
private var custom

Supports API

PHAssetCollection

@FetchAssetCollection(
    album: .smartAlbum,
    filter: NSPredicate(
        format: "NOT (assetCollectionSubtype IN %@)",
        [
            PHAssetCollectionSubtype.smartAlbumUserLibrary.rawValue,
            PHAssetCollectionSubtype.smartAlbumAllHidden.rawValue,
            PHAssetCollectionSubtype.smartAlbumFavorites.rawValue,
        ]
    )
) var albums

PHCollectionList

@FetchCollectionList(
    list: .smartFolder,
    kind: .any
) private var folders

PHAsset

// You can instantiate your results during init:
@FetchAssetList
private var assets: MediaResults<PHAsset>

// Or, if you already have a set of results from a `PHFetchResult<PHAsset>` instance:
@FetchAssetList(results)
private var assets

// Alternatively, if you have a `PHAssetCollection` instance:
@FetchAssetList(in: collection)
private var assets

There are multiple overrides for all of the above to allow for most customizations, alternatively you can pass a PHFetchOptions instance yourself as mentioned above.

Exclusions

The primary purpose of this library is to simplify the usage of the Photos framework in SwiftUI applications. As such, permission requests and mutations are not included.

Those APIs are not an issue that needs fixing by this library.

Instead you should continue to use the existing APIs available via the Photos framework. Since the use of Photos types is required, the library automatically imports Photos for you so adding an additional import is redundant.

Installation

The code is packaged as a framework. You can install manually (by copying the files in the Sources directory) or using Swift Package Manager (preferred)

To install using Swift Package Manager, add this to the dependencies section of your Package.swift file:

.package(url: "https://github.com/SwiftUI-Plus/Media.git", .upToNextMinor(from: "1.0.0"))

Note: The package requires iOS v13+

Other Packages

If you want easy access to this and more packages, add the following collection to your Xcode 13+ configuration:

https://benkau.com/packages.json

You might also like...
A SwiftUI app that reads iOS/watchOS/tvOS device model information from Xcode you select.
A SwiftUI app that reads iOS/watchOS/tvOS device model information from Xcode you select.

iVariant A SwiftUI app that reads iOS/watchOS/tvOS device model information from Xcode you select. Preview License iVariant is licensed under MIT. Cop

Pegase is a beautifully easy tool to keep track of your financial life on all your macOS
Pegase is a beautifully easy tool to keep track of your financial life on all your macOS

Pegase 🎉 Features 📒 Documentation Personal account software Pegase is a beautifully easy tool to keep track of your financial life on all your macOS

macOS application written in SwiftUI that downloads installer pkgs for the Install macOS Big Sur application.
macOS application written in SwiftUI that downloads installer pkgs for the Install macOS Big Sur application.

Download Full Installer This is a Swift UI implementation of my fetch-installer-pkg script. It will list the full macOS Big Sur (and later) installer

A SwiftUI wrapper around the `Add to Siri` button used to donate INIntents to the system.
A SwiftUI wrapper around the `Add to Siri` button used to donate INIntents to the system.

AddToSiri A SwiftUI wrapper around the Add to Siri button used to donate INIntents to the system. Originally created by Reddit user u/dippnerd (Github

A simple star rating library for SwiftUI apps on macOS and iOS

DLDRating A simple star rating library for SwiftUI apps on macOS and iOS. Features Installation Usage Styling Credits DLDRating was made by Dionne Lie

Visualize your dividend growth. DivRise tracks dividend prices of your stocks, gives you in-depth information about dividend paying stocks like the next dividend date and allows you to log your monthly dividend income.
Visualize your dividend growth. DivRise tracks dividend prices of your stocks, gives you in-depth information about dividend paying stocks like the next dividend date and allows you to log your monthly dividend income.

DivRise DivRise is an iOS app written in Pure SwiftUI that tracks dividend prices of your stocks, gives you in-depth information about dividend paying

🎲 100% SwiftUI 2.0, classic 2048 game [SwiftUI 2.0, iOS 14.0+, iPadOS 14.0+, macOS 11.0+, Swift 5.3].
🎲 100% SwiftUI 2.0, classic 2048 game [SwiftUI 2.0, iOS 14.0+, iPadOS 14.0+, macOS 11.0+, Swift 5.3].

swiftui-2048 If you like the project, please give it a star ⭐ It will show the creator your appreciation and help others to discover the repo. ✍️ Abou

xcode project wrapper around the Elixir TodoApp Desktop app to run on iOS
xcode project wrapper around the Elixir TodoApp Desktop app to run on iOS

TodoApp iOS: An iOS Sample App This xcode project wraps the Desktop Sample App to run on an iPhone. How to build & run Install xcode from the app stor

Native Jellyfin Client for iOS and tvOS
Native Jellyfin Client for iOS and tvOS

Swiftfin Swiftfin is a modern client for the Jellyfin media server. Redesigned in Swift to maximize direct play with the power of VLC and look native

Owner
SwiftUI+
A collection of SwiftUI backports and more. Fully certificate signed, so please do not use any of the packages if Xcode warns otherwise.
SwiftUI+
A new property wrapper for SwiftUI ObservableObject.

SharedObject ?? @SharedObject is an alternative to @StateObject, @ObservedObject, @EnvironmentObject to handle ObservableObject. If you need to have m

Lorenzo Fiamingo 51 Dec 8, 2022
Social Media platform build with swiftUI and Firebase with google and apple account integration for Signing In Users

Social Media platform build with swiftUI and Firebase with google and apple account integration for Signing In Users . Providing Users availability to upload posts and images add caption allowing other users to comment , with Find section to explore new people , new stories , User Profile section to allow the user to take control of his account .

Devang Papinwar 2 Jul 11, 2022
Short examples and references to work with property wrappers in SwiftUI.

A Colection of useful playgrounds This repo contains a collection of useful swift playgrounds. PropertyWrappers Playground Contains small example of u

Joan F. Guerrero 0 Nov 24, 2021
App which lets two people share their social media details by simply putting one phone on top of the other ("tapping"). Currently in development by Nikita Mounier.

Tap It Tap It enables two people to seamlessly share their social media information (e.g. Instagram, Snapchat, phone number) by simply placing one scr

Nikita Mounier 24 Oct 21, 2022
An iPhone Simulator "Wrapper" for SwiftUI Apps on macOS

SwiftUIPhone Run a SwiftUI app (or any SwiftUI view) in an iPhone Simulator "wrapper", directly on macOS! To be clear, this is not an iPhone Simulator

Justin Kaufman 7 May 20, 2022
Control Room : a macOS app that lets you control the simulators for iOS, tvOS, and watchOS

Control Room is a macOS app that lets you control the simulators for iOS, tvOS, and watchOS – their UI appearance, status bar configuration, and more.

null 0 Nov 30, 2021
SearchField for iOS, macOS and tvOS

SearchField for iOS, macOS and tvOS SearchField is created and maintaned with ❥ by Sascha Muellner. What? This is a SwiftUI package with support for i

Swift Package Repository 1 Jan 9, 2022
This is a sample app to create a photo selection classifier using CreateML on an iOS Device.

PhotoSelectionClassifier This is a sample app to create a photo selection classifier using CreateML on an iOS Device. Demo In the demo video below, we

null 15 Nov 18, 2022
iOS App that allows a pilot to check drone photo collection after a mapping mission for any issues

DronePhotoChecker iOS App that allows a pilot to check drone photo collection after a mapping mission for any issues. Features: Connects to DJI drone

null 0 Dec 30, 2021
Watch WWDC and Tech Talk Videos on your Apple TV, tvOS App.

WWDCTV This project allows you to watch WWDC & Tech Talks videos on your Apple TV. It has been built for tvOS 9.0 using Xcode 7.1 You can also favouri

Aaron Stephenson 421 Nov 3, 2022