├── .gitignore ├── Examples ├── PhotoPickerDemo.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── PhotoPickerDemo │ ├── Assets.xcassets │ ├── AccentColor.colorset │ │ └── Contents.json │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json │ ├── ContentView.swift │ ├── DemoApp.swift │ ├── Info.plist │ ├── LivePhotoView.swift │ └── Preview Content │ └── Preview Assets.xcassets │ └── Contents.json ├── LICENSE ├── Package.swift ├── README.md └── Sources └── PhotoPicker ├── Internals ├── DataLoader.swift ├── PHPickerResult+Load.swift ├── PhotoPickerError.swift ├── ZipMany.swift └── _PhotoPickerData.swift ├── PhotoPicker.swift ├── PhotoPickerData.swift └── PickerPattern.swift /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## User settings 6 | xcuserdata/ 7 | 8 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 9 | *.xcscmblueprint 10 | *.xccheckout 11 | 12 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 13 | build/ 14 | DerivedData/ 15 | *.moved-aside 16 | *.pbxuser 17 | !default.pbxuser 18 | *.mode1v3 19 | !default.mode1v3 20 | *.mode2v3 21 | !default.mode2v3 22 | *.perspectivev3 23 | !default.perspectivev3 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | 28 | ## App packaging 29 | *.ipa 30 | *.dSYM.zip 31 | *.dSYM 32 | 33 | ## Playgrounds 34 | timeline.xctimeline 35 | playground.xcworkspace 36 | 37 | # Swift Package Manager 38 | # 39 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 40 | # Packages/ 41 | # Package.pins 42 | # Package.resolved 43 | # *.xcodeproj 44 | # 45 | # Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata 46 | # hence it is not needed unless you have added a package configuration file to your project 47 | # .swiftpm 48 | 49 | .build/ 50 | 51 | # CocoaPods 52 | # 53 | # We recommend against adding the Pods directory to your .gitignore. However 54 | # you should judge for yourself, the pros and cons are mentioned at: 55 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 56 | # 57 | # Pods/ 58 | # 59 | # Add this line if you want to avoid checking in source code from the Xcode workspace 60 | # *.xcworkspace 61 | 62 | # Carthage 63 | # 64 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 65 | # Carthage/Checkouts 66 | 67 | Carthage/Build/ 68 | 69 | # Accio dependency management 70 | Dependencies/ 71 | .accio/ 72 | 73 | # fastlane 74 | # 75 | # It is recommended to not store the screenshots in the git repo. 76 | # Instead, use fastlane to re-generate the screenshots whenever they are needed. 77 | # For more information about the recommended setup visit: 78 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 79 | 80 | fastlane/report.xml 81 | fastlane/Preview.html 82 | fastlane/screenshots/**/*.png 83 | fastlane/test_output 84 | 85 | # Code Injection 86 | # 87 | # After new code Injection tools there's a generated folder /iOSInjectionProject 88 | # https://github.com/johnno1962/injectionforxcode 89 | 90 | iOSInjectionProject/ 91 | -------------------------------------------------------------------------------- /Examples/PhotoPickerDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 52; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 33F1521025DAAA8E006054A2 /* LivePhotoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33F1520F25DAAA8E006054A2 /* LivePhotoView.swift */; }; 11 | 33F264CA25DC05A100C1824B /* PhotoPicker in Frameworks */ = {isa = PBXBuildFile; productRef = 33F264C925DC05A100C1824B /* PhotoPicker */; }; 12 | 33FB018025D6BA5D0047B6F5 /* DemoApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33FB017F25D6BA5D0047B6F5 /* DemoApp.swift */; }; 13 | 33FB018225D6BA5D0047B6F5 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33FB018125D6BA5D0047B6F5 /* ContentView.swift */; }; 14 | 33FB018425D6BA5E0047B6F5 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 33FB018325D6BA5E0047B6F5 /* Assets.xcassets */; }; 15 | 33FB018725D6BA5E0047B6F5 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 33FB018625D6BA5E0047B6F5 /* Preview Assets.xcassets */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | 33F1520F25DAAA8E006054A2 /* LivePhotoView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LivePhotoView.swift; sourceTree = ""; }; 20 | 33F264C225DC051D00C1824B /* SwiftUI-PhotoPicker */ = {isa = PBXFileReference; lastKnownFileType = folder; name = "SwiftUI-PhotoPicker"; path = ..; sourceTree = ""; }; 21 | 33FB017C25D6BA5D0047B6F5 /* PhotoPickerDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PhotoPickerDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 22 | 33FB017F25D6BA5D0047B6F5 /* DemoApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoApp.swift; sourceTree = ""; }; 23 | 33FB018125D6BA5D0047B6F5 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 24 | 33FB018325D6BA5E0047B6F5 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 25 | 33FB018625D6BA5E0047B6F5 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 26 | 33FB018825D6BA5E0047B6F5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 27 | /* End PBXFileReference section */ 28 | 29 | /* Begin PBXFrameworksBuildPhase section */ 30 | 33FB017925D6BA5D0047B6F5 /* Frameworks */ = { 31 | isa = PBXFrameworksBuildPhase; 32 | buildActionMask = 2147483647; 33 | files = ( 34 | 33F264CA25DC05A100C1824B /* PhotoPicker in Frameworks */, 35 | ); 36 | runOnlyForDeploymentPostprocessing = 0; 37 | }; 38 | /* End PBXFrameworksBuildPhase section */ 39 | 40 | /* Begin PBXGroup section */ 41 | 33F264C425DC053600C1824B /* Frameworks */ = { 42 | isa = PBXGroup; 43 | children = ( 44 | ); 45 | name = Frameworks; 46 | sourceTree = ""; 47 | }; 48 | 33FB017325D6BA5D0047B6F5 = { 49 | isa = PBXGroup; 50 | children = ( 51 | 33F264C225DC051D00C1824B /* SwiftUI-PhotoPicker */, 52 | 33FB017E25D6BA5D0047B6F5 /* PhotoPickerDemo */, 53 | 33FB017D25D6BA5D0047B6F5 /* Products */, 54 | 33F264C425DC053600C1824B /* Frameworks */, 55 | ); 56 | sourceTree = ""; 57 | }; 58 | 33FB017D25D6BA5D0047B6F5 /* Products */ = { 59 | isa = PBXGroup; 60 | children = ( 61 | 33FB017C25D6BA5D0047B6F5 /* PhotoPickerDemo.app */, 62 | ); 63 | name = Products; 64 | sourceTree = ""; 65 | }; 66 | 33FB017E25D6BA5D0047B6F5 /* PhotoPickerDemo */ = { 67 | isa = PBXGroup; 68 | children = ( 69 | 33FB017F25D6BA5D0047B6F5 /* DemoApp.swift */, 70 | 33FB018125D6BA5D0047B6F5 /* ContentView.swift */, 71 | 33F1520F25DAAA8E006054A2 /* LivePhotoView.swift */, 72 | 33FB018325D6BA5E0047B6F5 /* Assets.xcassets */, 73 | 33FB018825D6BA5E0047B6F5 /* Info.plist */, 74 | 33FB018525D6BA5E0047B6F5 /* Preview Content */, 75 | ); 76 | path = PhotoPickerDemo; 77 | sourceTree = ""; 78 | }; 79 | 33FB018525D6BA5E0047B6F5 /* Preview Content */ = { 80 | isa = PBXGroup; 81 | children = ( 82 | 33FB018625D6BA5E0047B6F5 /* Preview Assets.xcassets */, 83 | ); 84 | path = "Preview Content"; 85 | sourceTree = ""; 86 | }; 87 | /* End PBXGroup section */ 88 | 89 | /* Begin PBXNativeTarget section */ 90 | 33FB017B25D6BA5D0047B6F5 /* PhotoPickerDemo */ = { 91 | isa = PBXNativeTarget; 92 | buildConfigurationList = 33FB018B25D6BA5E0047B6F5 /* Build configuration list for PBXNativeTarget "PhotoPickerDemo" */; 93 | buildPhases = ( 94 | 33FB017825D6BA5D0047B6F5 /* Sources */, 95 | 33FB017925D6BA5D0047B6F5 /* Frameworks */, 96 | 33FB017A25D6BA5D0047B6F5 /* Resources */, 97 | ); 98 | buildRules = ( 99 | ); 100 | dependencies = ( 101 | ); 102 | name = PhotoPickerDemo; 103 | packageProductDependencies = ( 104 | 33F264C925DC05A100C1824B /* PhotoPicker */, 105 | ); 106 | productName = PhotoPickerDemo; 107 | productReference = 33FB017C25D6BA5D0047B6F5 /* PhotoPickerDemo.app */; 108 | productType = "com.apple.product-type.application"; 109 | }; 110 | /* End PBXNativeTarget section */ 111 | 112 | /* Begin PBXProject section */ 113 | 33FB017425D6BA5D0047B6F5 /* Project object */ = { 114 | isa = PBXProject; 115 | attributes = { 116 | LastSwiftUpdateCheck = 1230; 117 | LastUpgradeCheck = 1230; 118 | TargetAttributes = { 119 | 33FB017B25D6BA5D0047B6F5 = { 120 | CreatedOnToolsVersion = 12.3; 121 | }; 122 | }; 123 | }; 124 | buildConfigurationList = 33FB017725D6BA5D0047B6F5 /* Build configuration list for PBXProject "PhotoPickerDemo" */; 125 | compatibilityVersion = "Xcode 9.3"; 126 | developmentRegion = en; 127 | hasScannedForEncodings = 0; 128 | knownRegions = ( 129 | en, 130 | Base, 131 | ); 132 | mainGroup = 33FB017325D6BA5D0047B6F5; 133 | productRefGroup = 33FB017D25D6BA5D0047B6F5 /* Products */; 134 | projectDirPath = ""; 135 | projectRoot = ""; 136 | targets = ( 137 | 33FB017B25D6BA5D0047B6F5 /* PhotoPickerDemo */, 138 | ); 139 | }; 140 | /* End PBXProject section */ 141 | 142 | /* Begin PBXResourcesBuildPhase section */ 143 | 33FB017A25D6BA5D0047B6F5 /* Resources */ = { 144 | isa = PBXResourcesBuildPhase; 145 | buildActionMask = 2147483647; 146 | files = ( 147 | 33FB018725D6BA5E0047B6F5 /* Preview Assets.xcassets in Resources */, 148 | 33FB018425D6BA5E0047B6F5 /* Assets.xcassets in Resources */, 149 | ); 150 | runOnlyForDeploymentPostprocessing = 0; 151 | }; 152 | /* End PBXResourcesBuildPhase section */ 153 | 154 | /* Begin PBXSourcesBuildPhase section */ 155 | 33FB017825D6BA5D0047B6F5 /* Sources */ = { 156 | isa = PBXSourcesBuildPhase; 157 | buildActionMask = 2147483647; 158 | files = ( 159 | 33FB018225D6BA5D0047B6F5 /* ContentView.swift in Sources */, 160 | 33FB018025D6BA5D0047B6F5 /* DemoApp.swift in Sources */, 161 | 33F1521025DAAA8E006054A2 /* LivePhotoView.swift in Sources */, 162 | ); 163 | runOnlyForDeploymentPostprocessing = 0; 164 | }; 165 | /* End PBXSourcesBuildPhase section */ 166 | 167 | /* Begin XCBuildConfiguration section */ 168 | 33FB018925D6BA5E0047B6F5 /* Debug */ = { 169 | isa = XCBuildConfiguration; 170 | buildSettings = { 171 | ALWAYS_SEARCH_USER_PATHS = NO; 172 | CLANG_ANALYZER_NONNULL = YES; 173 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 174 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 175 | CLANG_CXX_LIBRARY = "libc++"; 176 | CLANG_ENABLE_MODULES = YES; 177 | CLANG_ENABLE_OBJC_ARC = YES; 178 | CLANG_ENABLE_OBJC_WEAK = YES; 179 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 180 | CLANG_WARN_BOOL_CONVERSION = YES; 181 | CLANG_WARN_COMMA = YES; 182 | CLANG_WARN_CONSTANT_CONVERSION = YES; 183 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 184 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 185 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 186 | CLANG_WARN_EMPTY_BODY = YES; 187 | CLANG_WARN_ENUM_CONVERSION = YES; 188 | CLANG_WARN_INFINITE_RECURSION = YES; 189 | CLANG_WARN_INT_CONVERSION = YES; 190 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 191 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 192 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 193 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 194 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 195 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 196 | CLANG_WARN_STRICT_PROTOTYPES = YES; 197 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 198 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 199 | CLANG_WARN_UNREACHABLE_CODE = YES; 200 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 201 | COPY_PHASE_STRIP = NO; 202 | DEBUG_INFORMATION_FORMAT = dwarf; 203 | ENABLE_STRICT_OBJC_MSGSEND = YES; 204 | ENABLE_TESTABILITY = YES; 205 | GCC_C_LANGUAGE_STANDARD = gnu11; 206 | GCC_DYNAMIC_NO_PIC = NO; 207 | GCC_NO_COMMON_BLOCKS = YES; 208 | GCC_OPTIMIZATION_LEVEL = 0; 209 | GCC_PREPROCESSOR_DEFINITIONS = ( 210 | "DEBUG=1", 211 | "$(inherited)", 212 | ); 213 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 214 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 215 | GCC_WARN_UNDECLARED_SELECTOR = YES; 216 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 217 | GCC_WARN_UNUSED_FUNCTION = YES; 218 | GCC_WARN_UNUSED_VARIABLE = YES; 219 | IPHONEOS_DEPLOYMENT_TARGET = 14.3; 220 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 221 | MTL_FAST_MATH = YES; 222 | ONLY_ACTIVE_ARCH = YES; 223 | SDKROOT = iphoneos; 224 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 225 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 226 | }; 227 | name = Debug; 228 | }; 229 | 33FB018A25D6BA5E0047B6F5 /* Release */ = { 230 | isa = XCBuildConfiguration; 231 | buildSettings = { 232 | ALWAYS_SEARCH_USER_PATHS = NO; 233 | CLANG_ANALYZER_NONNULL = YES; 234 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 235 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 236 | CLANG_CXX_LIBRARY = "libc++"; 237 | CLANG_ENABLE_MODULES = YES; 238 | CLANG_ENABLE_OBJC_ARC = YES; 239 | CLANG_ENABLE_OBJC_WEAK = YES; 240 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 241 | CLANG_WARN_BOOL_CONVERSION = YES; 242 | CLANG_WARN_COMMA = YES; 243 | CLANG_WARN_CONSTANT_CONVERSION = YES; 244 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 245 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 246 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 247 | CLANG_WARN_EMPTY_BODY = YES; 248 | CLANG_WARN_ENUM_CONVERSION = YES; 249 | CLANG_WARN_INFINITE_RECURSION = YES; 250 | CLANG_WARN_INT_CONVERSION = YES; 251 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 252 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 253 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 254 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 255 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 256 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 257 | CLANG_WARN_STRICT_PROTOTYPES = YES; 258 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 259 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 260 | CLANG_WARN_UNREACHABLE_CODE = YES; 261 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 262 | COPY_PHASE_STRIP = NO; 263 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 264 | ENABLE_NS_ASSERTIONS = NO; 265 | ENABLE_STRICT_OBJC_MSGSEND = YES; 266 | GCC_C_LANGUAGE_STANDARD = gnu11; 267 | GCC_NO_COMMON_BLOCKS = YES; 268 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 269 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 270 | GCC_WARN_UNDECLARED_SELECTOR = YES; 271 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 272 | GCC_WARN_UNUSED_FUNCTION = YES; 273 | GCC_WARN_UNUSED_VARIABLE = YES; 274 | IPHONEOS_DEPLOYMENT_TARGET = 14.3; 275 | MTL_ENABLE_DEBUG_INFO = NO; 276 | MTL_FAST_MATH = YES; 277 | SDKROOT = iphoneos; 278 | SWIFT_COMPILATION_MODE = wholemodule; 279 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 280 | VALIDATE_PRODUCT = YES; 281 | }; 282 | name = Release; 283 | }; 284 | 33FB018C25D6BA5E0047B6F5 /* Debug */ = { 285 | isa = XCBuildConfiguration; 286 | buildSettings = { 287 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 288 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 289 | CODE_SIGN_STYLE = Automatic; 290 | DEVELOPMENT_ASSET_PATHS = "\"PhotoPickerDemo/Preview Content\""; 291 | DEVELOPMENT_TEAM = UMBZ5WL247; 292 | ENABLE_PREVIEWS = YES; 293 | INFOPLIST_FILE = PhotoPickerDemo/Info.plist; 294 | IPHONEOS_DEPLOYMENT_TARGET = 14.0; 295 | LD_RUNPATH_SEARCH_PATHS = ( 296 | "$(inherited)", 297 | "@executable_path/Frameworks", 298 | ); 299 | PRODUCT_BUNDLE_IDENTIFIER = com.inamiy.PhotoPickerDemo; 300 | PRODUCT_NAME = "$(TARGET_NAME)"; 301 | SWIFT_VERSION = 5.0; 302 | TARGETED_DEVICE_FAMILY = "1,2"; 303 | }; 304 | name = Debug; 305 | }; 306 | 33FB018D25D6BA5E0047B6F5 /* Release */ = { 307 | isa = XCBuildConfiguration; 308 | buildSettings = { 309 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 310 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 311 | CODE_SIGN_STYLE = Automatic; 312 | DEVELOPMENT_ASSET_PATHS = "\"PhotoPickerDemo/Preview Content\""; 313 | DEVELOPMENT_TEAM = UMBZ5WL247; 314 | ENABLE_PREVIEWS = YES; 315 | INFOPLIST_FILE = PhotoPickerDemo/Info.plist; 316 | IPHONEOS_DEPLOYMENT_TARGET = 14.0; 317 | LD_RUNPATH_SEARCH_PATHS = ( 318 | "$(inherited)", 319 | "@executable_path/Frameworks", 320 | ); 321 | PRODUCT_BUNDLE_IDENTIFIER = com.inamiy.PhotoPickerDemo; 322 | PRODUCT_NAME = "$(TARGET_NAME)"; 323 | SWIFT_VERSION = 5.0; 324 | TARGETED_DEVICE_FAMILY = "1,2"; 325 | }; 326 | name = Release; 327 | }; 328 | /* End XCBuildConfiguration section */ 329 | 330 | /* Begin XCConfigurationList section */ 331 | 33FB017725D6BA5D0047B6F5 /* Build configuration list for PBXProject "PhotoPickerDemo" */ = { 332 | isa = XCConfigurationList; 333 | buildConfigurations = ( 334 | 33FB018925D6BA5E0047B6F5 /* Debug */, 335 | 33FB018A25D6BA5E0047B6F5 /* Release */, 336 | ); 337 | defaultConfigurationIsVisible = 0; 338 | defaultConfigurationName = Release; 339 | }; 340 | 33FB018B25D6BA5E0047B6F5 /* Build configuration list for PBXNativeTarget "PhotoPickerDemo" */ = { 341 | isa = XCConfigurationList; 342 | buildConfigurations = ( 343 | 33FB018C25D6BA5E0047B6F5 /* Debug */, 344 | 33FB018D25D6BA5E0047B6F5 /* Release */, 345 | ); 346 | defaultConfigurationIsVisible = 0; 347 | defaultConfigurationName = Release; 348 | }; 349 | /* End XCConfigurationList section */ 350 | 351 | /* Begin XCSwiftPackageProductDependency section */ 352 | 33F264C925DC05A100C1824B /* PhotoPicker */ = { 353 | isa = XCSwiftPackageProductDependency; 354 | productName = PhotoPicker; 355 | }; 356 | /* End XCSwiftPackageProductDependency section */ 357 | }; 358 | rootObject = 33FB017425D6BA5D0047B6F5 /* Project object */; 359 | } 360 | -------------------------------------------------------------------------------- /Examples/PhotoPickerDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Examples/PhotoPickerDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Examples/PhotoPickerDemo/Assets.xcassets/AccentColor.colorset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "colors" : [ 3 | { 4 | "idiom" : "universal" 5 | } 6 | ], 7 | "info" : { 8 | "author" : "xcode", 9 | "version" : 1 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Examples/PhotoPickerDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "scale" : "2x", 6 | "size" : "20x20" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "scale" : "3x", 11 | "size" : "20x20" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "scale" : "2x", 16 | "size" : "29x29" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "scale" : "3x", 21 | "size" : "29x29" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "scale" : "2x", 26 | "size" : "40x40" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "scale" : "3x", 31 | "size" : "40x40" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "scale" : "2x", 36 | "size" : "60x60" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "scale" : "3x", 41 | "size" : "60x60" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "scale" : "1x", 46 | "size" : "20x20" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "scale" : "2x", 51 | "size" : "20x20" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "scale" : "1x", 56 | "size" : "29x29" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "scale" : "2x", 61 | "size" : "29x29" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "scale" : "1x", 66 | "size" : "40x40" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "scale" : "2x", 71 | "size" : "40x40" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "scale" : "1x", 76 | "size" : "76x76" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "scale" : "2x", 81 | "size" : "76x76" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "scale" : "2x", 86 | "size" : "83.5x83.5" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "scale" : "1x", 91 | "size" : "1024x1024" 92 | } 93 | ], 94 | "info" : { 95 | "author" : "xcode", 96 | "version" : 1 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /Examples/PhotoPickerDemo/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Examples/PhotoPickerDemo/ContentView.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | import PhotosUI 3 | import Combine 4 | import AVKit 5 | import PhotoPicker 6 | 7 | struct ContentView: View 8 | { 9 | @State 10 | private var datas: [PhotoPickerData?] = [] 11 | 12 | @State 13 | private var isShowingPicker = false 14 | 15 | var body: some View 16 | { 17 | VStack(spacing: 40) { 18 | Spacer(minLength: 40) 19 | 20 | VStack(spacing: 20) { 21 | Button("Select Image") { 22 | self.isShowingPicker = true 23 | } 24 | 25 | Button("Log") { 26 | print(datas) 27 | } 28 | } 29 | 30 | ScrollView { 31 | LazyVGrid(columns: Array(repeating: GridItem(), count: 2)) { 32 | ForEach(datas.enumerated().map { ($0, $1) }, id: \.0) { i, data in 33 | if let image = data?.image { 34 | Image(uiImage: image) 35 | .resizable() 36 | .scaledToFit() 37 | } 38 | else if let videoURL = data?.video { 39 | VideoPlayer(player: AVPlayer(url: videoURL)) 40 | .frame(width: 200, height: 200, alignment: .center) 41 | } 42 | else if let livePhoto = data?.livePhoto { 43 | LivePhotoView(livePhoto: .constant(livePhoto)) 44 | } 45 | } 46 | } 47 | } 48 | } 49 | .sheet(isPresented: $isShowingPicker) { 50 | PhotoPicker( 51 | datas: $datas, 52 | configuration: pickerConfig, 53 | pattern: pickerPattern 54 | ) 55 | } 56 | } 57 | } 58 | 59 | private let pickerPattern: PickerPattern = .any(of: [.images, .videos, .livePhotos]) 60 | 61 | private let pickerConfig: PHPickerConfiguration = { 62 | var config = PHPickerConfiguration() 63 | config.filter = pickerPattern.filter 64 | config.selectionLimit = 0 65 | config.preferredAssetRepresentationMode = .current // required for video 66 | return config 67 | }() 68 | 69 | // MARK: - Previews 70 | 71 | struct ContentView_Previews: PreviewProvider 72 | { 73 | static var previews: some View { 74 | ContentView() 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /Examples/PhotoPickerDemo/DemoApp.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | @main 4 | struct DemoApp: App 5 | { 6 | var body: some Scene 7 | { 8 | WindowGroup { 9 | ContentView() 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Examples/PhotoPickerDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | $(PRODUCT_BUNDLE_PACKAGE_TYPE) 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UIApplicationSceneManifest 24 | 25 | UIApplicationSupportsMultipleScenes 26 | 27 | 28 | UIApplicationSupportsIndirectInputEvents 29 | 30 | UILaunchScreen 31 | 32 | UIRequiredDeviceCapabilities 33 | 34 | armv7 35 | 36 | UISupportedInterfaceOrientations 37 | 38 | UIInterfaceOrientationPortrait 39 | UIInterfaceOrientationLandscapeLeft 40 | UIInterfaceOrientationLandscapeRight 41 | 42 | UISupportedInterfaceOrientations~ipad 43 | 44 | UIInterfaceOrientationPortrait 45 | UIInterfaceOrientationPortraitUpsideDown 46 | UIInterfaceOrientationLandscapeLeft 47 | UIInterfaceOrientationLandscapeRight 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /Examples/PhotoPickerDemo/LivePhotoView.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | import PhotosUI 3 | 4 | struct LivePhotoView: UIViewRepresentable 5 | { 6 | @Binding 7 | var livePhoto: PHLivePhoto? 8 | 9 | func makeUIView(context: Context) -> PHLivePhotoView 10 | { 11 | PHLivePhotoView() 12 | } 13 | 14 | func updateUIView(_ lpView: PHLivePhotoView, context: Context) 15 | { 16 | lpView.livePhoto = livePhoto 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Examples/PhotoPickerDemo/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Yasuhiro Inami 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.3 2 | 3 | import PackageDescription 4 | 5 | let package = Package( 6 | name: "SwiftUI-PhotoPicker", 7 | platforms: [.iOS(.v14)], 8 | products: [ 9 | .library( 10 | name: "PhotoPicker", 11 | targets: ["PhotoPicker"]), 12 | ], 13 | dependencies: [], 14 | targets: [ 15 | .target( 16 | name: "PhotoPicker", 17 | dependencies: []), 18 | ] 19 | ) 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SwiftUI-PhotoPicker 2 | iOS 14 PHPickerViewController wrapper for SwiftUI with data loader support. 3 | 4 | ## How to Use 5 | 6 | ```swift 7 | struct ContentView: View 8 | { 9 | @State 10 | private var datas: [PhotoPickerData?] = [] 11 | 12 | @State 13 | private var isShowingPicker = false 14 | 15 | var body: some View 16 | { 17 | VStack(spacing: 40) { 18 | Button("Select Image") { 19 | self.isShowingPicker = true 20 | } 21 | 22 | ScrollView { 23 | LazyVGrid(columns: Array(repeating: GridItem(), count: 2)) { 24 | ForEach(datas.enumerated().map { ($0, $1) }, id: \.0) { i, data in 25 | if let image = data?.image { 26 | Image(uiImage: image) 27 | .resizable() 28 | .scaledToFit() 29 | } 30 | else if let videoURL = data?.video { 31 | VideoPlayer(player: AVPlayer(url: videoURL)) 32 | .frame(width: 200, height: 200, alignment: .center) 33 | } 34 | else if let livePhoto = data?.livePhoto { 35 | LivePhotoView(livePhoto: .constant(livePhoto)) 36 | } 37 | } 38 | } 39 | } 40 | } 41 | .sheet(isPresented: $isShowingPicker) { 42 | PhotoPicker( 43 | datas: $datas, 44 | configuration: pickerConfig, 45 | pattern: pickerPattern 46 | ) 47 | } 48 | } 49 | } 50 | 51 | private let pickerPattern: PickerPattern = .any(of: [.images, .videos, .livePhotos]) 52 | 53 | private let pickerConfig: PHPickerConfiguration = { 54 | var config = PHPickerConfiguration() 55 | config.filter = pickerPattern.filter 56 | config.selectionLimit = 0 57 | config.preferredAssetRepresentationMode = .current // required for video 58 | return config 59 | }() 60 | ``` 61 | 62 | ## Example 63 | 64 | 65 | 66 | See [Examples](Examples) for more information. 67 | 68 | ## License 69 | 70 | [MIT](LICENSE) 71 | -------------------------------------------------------------------------------- /Sources/PhotoPicker/Internals/DataLoader.swift: -------------------------------------------------------------------------------- 1 | import PhotosUI 2 | import Combine 3 | 4 | /// Simple wrapper for async data-loading publisher to change its output type to `PhotoPickerData`. 5 | struct DataLoader 6 | { 7 | let publisher: (PHPickerResult) -> AnyPublisher 8 | 9 | public init

( 10 | _ publisher: @escaping (PHPickerResult) -> P 11 | ) where 12 | P: Publisher, P.Output: PhotoPickerData, P.Failure == PhotoPickerError 13 | { 14 | self.publisher = { publisher($0).map { $0 as PhotoPickerData }.eraseToAnyPublisher() } 15 | } 16 | 17 | static let image = DataLoader { $0.imagePublisher.map { _PhotoPickerData.image($0) } } 18 | static let video = DataLoader { $0.videoPublisher.map { _PhotoPickerData.video($0) } } 19 | static let livePhoto = DataLoader { $0.livePhotoPublisher.map { _PhotoPickerData.livePhoto($0) } } 20 | } 21 | -------------------------------------------------------------------------------- /Sources/PhotoPicker/Internals/PHPickerResult+Load.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | import PhotosUI 3 | import Combine 4 | 5 | // MARK: - loadObject 6 | 7 | extension PHPickerResult 8 | { 9 | func loadObject( 10 | _ type: T.Type = T.self, 11 | completion: @escaping (Result) -> Void 12 | ) 13 | { 14 | let provider = self.itemProvider 15 | 16 | guard provider.canLoadObject(ofClass: T.self) else { 17 | completion(.failure(.loadDataFailed(reason: "`canLoadObject(ofClass: \(T.self))` failed."))) 18 | return 19 | } 20 | 21 | provider.loadObject(ofClass: T.self) { image, error in 22 | if let image = image as? T { 23 | completion(.success(image)) 24 | } 25 | else if let error = error { 26 | completion(.failure(.underlyingError(error))) 27 | } 28 | else { 29 | assertionFailure() 30 | } 31 | } 32 | } 33 | 34 | func objectPublisher( 35 | _ type: T.Type = T.self 36 | ) -> AnyPublisher 37 | { 38 | Deferred { 39 | Future { completion in 40 | self.loadObject(type, completion: completion) 41 | } 42 | } 43 | .eraseToAnyPublisher() 44 | } 45 | } 46 | 47 | // MARK: - UIImage 48 | 49 | extension PHPickerResult 50 | { 51 | func loadImage(completion: @escaping (Result) -> Void) 52 | { 53 | self.loadObject(completion: completion) 54 | } 55 | 56 | var imagePublisher: AnyPublisher 57 | { 58 | self.objectPublisher() 59 | } 60 | } 61 | 62 | // MARK: - Video 63 | 64 | extension PHPickerResult 65 | { 66 | func loadVideo(completion: @escaping (Result) -> Void) 67 | { 68 | let provider = self.itemProvider 69 | 70 | guard let typeIdentifier = provider.registeredTypeIdentifiers.first else { 71 | completion(.failure(.loadDataFailed(reason: "No `registeredTypeIdentifiers` while `loadVideo`."))) 72 | return 73 | } 74 | 75 | // NOTE: Required to call first. 76 | provider.loadFileRepresentation(forTypeIdentifier: typeIdentifier) { url, error in 77 | if let error = error { 78 | completion(.failure(.underlyingError(error))) 79 | return 80 | } 81 | 82 | provider.loadItem(forTypeIdentifier: typeIdentifier, options: nil) { (url, error) in 83 | if let url = url as? URL { 84 | completion(.success(url)) 85 | } 86 | else if let error = error { 87 | completion(.failure(.underlyingError(error))) 88 | } 89 | else { 90 | assertionFailure() 91 | } 92 | } 93 | } 94 | } 95 | 96 | var videoPublisher: AnyPublisher 97 | { 98 | Deferred { 99 | Future(self.loadVideo(completion:)) 100 | } 101 | .eraseToAnyPublisher() 102 | } 103 | } 104 | 105 | // MARK: - LivePhoto 106 | 107 | extension PHPickerResult 108 | { 109 | func getLivePhoto(completion: @escaping (Result) -> Void) 110 | { 111 | self.loadObject(completion: completion) 112 | } 113 | 114 | var livePhotoPublisher: AnyPublisher 115 | { 116 | self.objectPublisher() 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /Sources/PhotoPicker/Internals/PhotoPickerError.swift: -------------------------------------------------------------------------------- 1 | enum PhotoPickerError: Swift.Error 2 | { 3 | case loadDataFailed(reason: String) 4 | case underlyingError(Swift.Error) 5 | } 6 | -------------------------------------------------------------------------------- /Sources/PhotoPicker/Internals/ZipMany.swift: -------------------------------------------------------------------------------- 1 | import Combine 2 | 3 | extension Publishers 4 | { 5 | struct ZipMany: Publisher where Upstream: Publisher 6 | { 7 | typealias Output = [Upstream.Output] 8 | typealias Failure = Upstream.Failure 9 | 10 | private let upstreams: [Upstream] 11 | 12 | init(_ upstreams: [Upstream]) 13 | { 14 | self.upstreams = upstreams 15 | } 16 | 17 | func receive(subscriber: S) 18 | where S: Subscriber, Self.Failure == S.Failure, Self.Output == S.Input 19 | { 20 | let initial = Just<[Upstream.Output]>([]) 21 | .setFailureType(to: Failure.self) 22 | .eraseToAnyPublisher() 23 | 24 | let zipped = upstreams.reduce(into: initial) { result, upstream in 25 | result = result.zip(upstream) { elements, element in 26 | elements + [element] 27 | } 28 | .eraseToAnyPublisher() 29 | } 30 | 31 | zipped.subscribe(subscriber) 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Sources/PhotoPicker/Internals/_PhotoPickerData.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | import Photos 3 | 4 | enum _PhotoPickerData: PhotoPickerData 5 | { 6 | case image(UIImage) 7 | case video(URL?) 8 | case livePhoto(PHLivePhoto) 9 | 10 | var image: UIImage? 11 | { 12 | guard case let .image(value) = self else { return nil } 13 | return value 14 | } 15 | 16 | var video: URL? 17 | { 18 | guard case let .video(value) = self else { return nil } 19 | return value 20 | } 21 | 22 | var livePhoto: PHLivePhoto? 23 | { 24 | guard case let .livePhoto(value) = self else { return nil } 25 | return value 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Sources/PhotoPicker/PhotoPicker.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | import PhotosUI 3 | import Combine 4 | 5 | /// `PHPickerViewController` wrapper for SwiftUI. 6 | public struct PhotoPicker: UIViewControllerRepresentable 7 | { 8 | @Binding 9 | public var datas: [PhotoPickerData?] 10 | 11 | private let configuration: PHPickerConfiguration 12 | private let pattern: PickerPattern 13 | 14 | @Environment(\.presentationMode) 15 | private var presentationMode 16 | 17 | public init( 18 | datas: Binding<[PhotoPickerData?]>, 19 | configuration: PHPickerConfiguration, 20 | pattern: PickerPattern 21 | ) 22 | { 23 | self._datas = datas 24 | self.configuration = configuration 25 | self.pattern = pattern 26 | } 27 | 28 | public func makeUIViewController(context: Context) -> PHPickerViewController 29 | { 30 | let vc = PHPickerViewController(configuration: configuration) 31 | vc.delegate = context.coordinator 32 | return vc 33 | } 34 | 35 | public func updateUIViewController( 36 | _ uiViewController: PHPickerViewController, 37 | context: Context 38 | ) 39 | { 40 | 41 | } 42 | 43 | public func makeCoordinator() -> Coordinator 44 | { 45 | Coordinator(self) 46 | } 47 | 48 | // MARK: - Coordinator 49 | 50 | public class Coordinator: PHPickerViewControllerDelegate 51 | { 52 | private let parent: PhotoPicker 53 | 54 | private var cancellables: Set = .init() 55 | 56 | init(_ parent: PhotoPicker) 57 | { 58 | self.parent = parent 59 | } 60 | 61 | public func picker( 62 | _ picker: PHPickerViewController, 63 | didFinishPicking results: [PHPickerResult] 64 | ) 65 | { 66 | if results.isEmpty { 67 | self.parent.presentationMode.wrappedValue.dismiss() 68 | self.parent.datas = [] 69 | return 70 | } 71 | 72 | parent.pattern.makePublisher(results: results) 73 | .sink(receiveValue: { (datas: [PhotoPickerData?]) in 74 | self.parent.presentationMode.wrappedValue.dismiss() 75 | self.parent.datas = datas 76 | }) 77 | .store(in: &cancellables) 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Sources/PhotoPicker/PhotoPickerData.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | import PhotosUI 3 | 4 | /// Extensible picker data Interface. 5 | public protocol PhotoPickerData 6 | { 7 | var image: UIImage? { get } 8 | var video: URL? { get } 9 | var livePhoto: PHLivePhoto? { get } 10 | } 11 | 12 | extension PhotoPickerData 13 | { 14 | public var image: UIImage? { nil } 15 | public var video: URL? { nil } 16 | public var livePhoto: PHLivePhoto? { nil } 17 | } 18 | -------------------------------------------------------------------------------- /Sources/PhotoPicker/PickerPattern.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | import PhotosUI 3 | import Combine 4 | 5 | // MARK: - PickerPattern 6 | 7 | /// `PHPickerFilter` filter pattern with data-loading support. 8 | public struct PickerPattern 9 | { 10 | public let filter: PHPickerFilter 11 | private let dataLoaders: [DataLoader] 12 | 13 | private init( 14 | filter: PHPickerFilter, 15 | dataLoaders: [DataLoader] 16 | ) 17 | { 18 | self.filter = filter 19 | self.dataLoaders = dataLoaders 20 | } 21 | 22 | func makePublisher(results: [PHPickerResult]) -> AnyPublisher<[PhotoPickerData?], Never> 23 | { 24 | toAnyOf(dataLoaders: self.dataLoaders, results: results) 25 | } 26 | } 27 | 28 | // MARK: - PickerPattern Presets 29 | 30 | extension PickerPattern 31 | { 32 | public static let images = PickerPattern(filter: .images, dataLoaders: [.image]) 33 | public static let videos = PickerPattern(filter: .videos, dataLoaders: [.video]) 34 | public static let livePhotos = PickerPattern(filter: .livePhotos, dataLoaders: [.livePhoto]) 35 | 36 | public static func any(of patterns: [PickerPattern]) -> PickerPattern 37 | { 38 | PickerPattern( 39 | filter: .any(of: patterns.map { $0.filter }), 40 | dataLoaders: patterns.reduce(into: []) { $0 += $1.dataLoaders } 41 | ) 42 | } 43 | } 44 | 45 | // MARK: - Private 46 | 47 | /// Loads picker data using `DatadataLoader`s. 48 | private func toAnyOf( 49 | dataLoaders: [DataLoader], 50 | results: [PHPickerResult] 51 | ) -> AnyPublisher<[PhotoPickerData?], Never> 52 | { 53 | typealias Publisher = AnyPublisher 54 | 55 | /// e.g. `[results.map(\.imagePublisher), results.map(\.videoPublisher)]` 56 | let publishersArray: [[Publisher]] = dataLoaders 57 | .map { pattern in 58 | results.map { result -> Publisher in 59 | pattern.publisher(result) 60 | .map { Optional($0) } 61 | .catch { _ in Just(nil) } 62 | .eraseToAnyPublisher() 63 | } 64 | } 65 | 66 | /// e.g. `[ [Just(nil), Just(nil), ... (num of results)], [Just(nil), ...], ... (num of dataLoaders) ]` 67 | let initial: [Publisher] = [Publisher]( 68 | repeating: Just(nil).eraseToAnyPublisher(), 69 | count: results.count 70 | ) 71 | 72 | let publishers = publishersArray 73 | .reduce(into: initial) { (results: inout [Publisher], arrayOfPublishers: [Publisher]) in 74 | results = zip(results, arrayOfPublishers) 75 | .map { publisher1, publisher2 in 76 | publisher1 77 | .flatMap { (result1: PhotoPickerData?) -> Publisher in 78 | if let result1 = result1 { 79 | return Result.Publisher(result1).eraseToAnyPublisher() 80 | } 81 | else { 82 | return publisher2.eraseToAnyPublisher() 83 | } 84 | } 85 | .eraseToAnyPublisher() 86 | } 87 | } 88 | 89 | return Publishers.ZipMany(publishers) 90 | .eraseToAnyPublisher() 91 | } 92 | --------------------------------------------------------------------------------