Update ios example
This commit is contained in:
parent
2ce0b6f3bf
commit
1dc476e7f5
|
@ -7,6 +7,7 @@
|
|||
objects = {
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
F4A9D56421D5FE6300A6E2DE /* PhotosLibrary.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4A9D56321D5FE6300A6E2DE /* PhotosLibrary.swift */; };
|
||||
F4EC51C421D33B6900DED569 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4EC51C321D33B6900DED569 /* AppDelegate.swift */; };
|
||||
F4EC51C621D33B6900DED569 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4EC51C521D33B6900DED569 /* ViewController.swift */; };
|
||||
F4EC51CB21D33B6B00DED569 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F4EC51CA21D33B6B00DED569 /* Assets.xcassets */; };
|
||||
|
@ -61,6 +62,7 @@
|
|||
/* End PBXCopyFilesBuildPhase section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
F4A9D56321D5FE6300A6E2DE /* PhotosLibrary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhotosLibrary.swift; sourceTree = "<group>"; };
|
||||
F4EC51C021D33B6900DED569 /* ImagesExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ImagesExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
F4EC51C321D33B6900DED569 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
|
||||
F4EC51C521D33B6900DED569 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
|
||||
|
@ -119,6 +121,7 @@
|
|||
F4EC51CA21D33B6B00DED569 /* Assets.xcassets */,
|
||||
F4EC51CC21D33B6B00DED569 /* LaunchScreen.storyboard */,
|
||||
F4EC51CF21D33B6B00DED569 /* Info.plist */,
|
||||
F4A9D56321D5FE6300A6E2DE /* PhotosLibrary.swift */,
|
||||
);
|
||||
path = ImagesExample;
|
||||
sourceTree = "<group>";
|
||||
|
@ -274,6 +277,7 @@
|
|||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
F4EC51C621D33B6900DED569 /* ViewController.swift in Sources */,
|
||||
F4A9D56421D5FE6300A6E2DE /* PhotosLibrary.swift in Sources */,
|
||||
F4EC51C421D33B6900DED569 /* AppDelegate.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
|
|
|
@ -20,6 +20,8 @@
|
|||
<string>1</string>
|
||||
<key>LSRequiresIPhoneOS</key>
|
||||
<true/>
|
||||
<key>NSPhotoLibraryUsageDescription</key>
|
||||
<string>Add or remove placeholder images into your camera roll</string>
|
||||
<key>UILaunchStoryboardName</key>
|
||||
<string>LaunchScreen</string>
|
||||
<key>UIRequiredDeviceCapabilities</key>
|
||||
|
|
|
@ -0,0 +1,91 @@
|
|||
//
|
||||
// PhotosLibrary.swift
|
||||
// ImagesExample
|
||||
//
|
||||
// Created by Simon Lee on 12/28/18.
|
||||
// Copyright © 2018 Shao Ping Lee. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Photos
|
||||
|
||||
class PhotosLibrary {
|
||||
enum Error: Swift.Error {
|
||||
case AlbumDNE
|
||||
}
|
||||
|
||||
static let albumName = "PlaceholderKit"
|
||||
|
||||
func addImages(_ images: [UIImage]) throws {
|
||||
for image in images {
|
||||
try addImage(image)
|
||||
}
|
||||
}
|
||||
|
||||
// Add image to thelibrary
|
||||
func addImage(_ image: UIImage,
|
||||
toAlbumNamed title: String = albumName) throws {
|
||||
guard let album = fetchAlbum(title: title).firstObject else {
|
||||
throw Error.AlbumDNE
|
||||
}
|
||||
|
||||
let assetChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
|
||||
let assetPlaceHolder = assetChangeRequest.placeholderForCreatedAsset
|
||||
let albumChangeRequest = PHAssetCollectionChangeRequest(for: album)
|
||||
let enumeration = [assetPlaceHolder].compactMap({$0}) as NSArray
|
||||
albumChangeRequest?.addAssets(enumeration)
|
||||
}
|
||||
|
||||
func fetchAlbum(title: String) -> PHFetchResult<PHAssetCollection> {
|
||||
// Fetch albums with title
|
||||
let options = PHFetchOptions()
|
||||
options.predicate = NSPredicate(format: "title = %@", title)
|
||||
let album = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: options)
|
||||
return album
|
||||
}
|
||||
|
||||
func performChanges(_ changeBlock: @escaping (PhotosLibrary) -> Void,
|
||||
completionHandler: ((Bool, Swift.Error?) -> Void)? = nil) {
|
||||
ensurePhotoLibaryAuthorized { (library) in
|
||||
PHPhotoLibrary.shared().performChanges({
|
||||
changeBlock(library)
|
||||
}, completionHandler: completionHandler)
|
||||
}
|
||||
}
|
||||
|
||||
func ensurePhotoLibaryAuthorized(authorizedHandler: @escaping (PhotosLibrary) -> Void) {
|
||||
if PHPhotoLibrary.authorizationStatus() == .authorized {
|
||||
authorizedHandler(self)
|
||||
return
|
||||
}
|
||||
|
||||
PHPhotoLibrary.requestAuthorization { _ in
|
||||
authorizedHandler(self)
|
||||
}
|
||||
}
|
||||
|
||||
// Create an album for placeholders
|
||||
func createAlbum(title: String = albumName) {
|
||||
// Make sure album doesn't exist already
|
||||
let album = fetchAlbum(title: title)
|
||||
guard album.count == 0 else {
|
||||
return
|
||||
}
|
||||
|
||||
// Create a new album with the entered title.
|
||||
PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: title)
|
||||
}
|
||||
|
||||
// Remove the album the has the placeholders
|
||||
func removeAlbum(title: String = albumName) {
|
||||
|
||||
let album = fetchAlbum(title: title)
|
||||
|
||||
// Delete
|
||||
album.enumerateObjects { (collection, _, _) in
|
||||
let assets = PHAsset.fetchAssets(in: collection, options: nil)
|
||||
PHAssetChangeRequest.deleteAssets(assets)
|
||||
PHAssetCollectionChangeRequest(for: collection)?.removeAssets(assets)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,16 +8,19 @@
|
|||
|
||||
import UIKit
|
||||
import PlaceholderKit
|
||||
import Photos
|
||||
|
||||
class ViewController: UIViewController {
|
||||
var sampleImagesCollectionViewController: SampleImagesCollectionViewController?
|
||||
|
||||
let images: [UIImage] = PlaceholderKit.defaultPlaceholders
|
||||
let photosLibary = PhotosLibrary()
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
view.backgroundColor = UIColor(red: 0.3, green: 0.3, blue: 0.3, alpha: 1.0)
|
||||
setupCollectionViewController()
|
||||
|
||||
}
|
||||
|
||||
func setupCollectionViewController() {
|
||||
|
@ -29,12 +32,25 @@ class ViewController: UIViewController {
|
|||
sampleImagesCollectionViewController = controller
|
||||
|
||||
NSLayoutConstraint.activate([
|
||||
collectionView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 30),
|
||||
collectionView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -30),
|
||||
collectionView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
|
||||
collectionView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
|
||||
collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -30),
|
||||
collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 30)
|
||||
])
|
||||
}
|
||||
|
||||
func insertImagesIntoPhotos(completionHandler: ((Bool, Error?) -> Void)? = nil) {
|
||||
photosLibary.performChanges({ library in
|
||||
library.createAlbum()
|
||||
try? library.addImages(self.images)
|
||||
}, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
func removeAlbum(completionHandler: ((Bool, Error?) -> Void)? = nil) {
|
||||
photosLibary.performChanges({ library in
|
||||
library.removeAlbum()
|
||||
}, completionHandler: completionHandler)
|
||||
}
|
||||
}
|
||||
|
||||
class SampleImagesCollectionViewController: UICollectionViewController {
|
||||
|
|
Loading…
Reference in New Issue