import PhotoEditorSDK
import UIKit
class PhotoAddFiltersFromRemoteURLSwift: Example, PhotoEditViewControllerDelegate {
private var downloadLocations = [String: URL]()
override func invokeExample() {
let photo = Photo(url: Bundle.main.url(forResource: "LA", withExtension: "jpg")!)
let filterFilename = "custom_lut_invert.png"
let thumbnailFilename = "custom_filter_category.jpg"
let assetFilenames = [filterFilename, thumbnailFilename]
assetFilenames.forEach { filename in
guard let remoteURL = URL(string: "https://img.ly/static/example-assets/\(filename)") else {
fatalError("Unable to parse URL.")
}
let downloadTask = URLSession.shared.downloadTask(with: remoteURL) { downloadURL, _, error in
if let error = error {
fatalError("There was an error downloading the file: \(error)")
}
guard let downloadURL = downloadURL else {
return
}
let temporaryDirectoryURL = FileManager.default.temporaryDirectory
let localURL = temporaryDirectoryURL.appendingPathComponent(filename)
if FileManager.default.fileExists(atPath: localURL.path) {
try? FileManager.default.removeItem(at: localURL)
}
try? FileManager.default.moveItem(at: downloadURL, to: localURL)
DispatchQueue.main.async {
self.downloadLocations[filename] = localURL
if self.downloadLocations.count == assetFilenames.count {
let filterURL = self.downloadLocations[filterFilename]
let customLUTFilter = LUTEffect(identifier: "custom_lut_filter", lutURL: filterURL!, displayName: "Invert", horizontalTileCount: 5, verticalTileCount: 5)
let customDuoToneFilter = DuoToneEffect(identifier: "custom_duotone_filter", lightColor: .yellow, darkColor: .blue, displayName: "Custom")
let configuration = Configuration { builder in
let assetCatalog = AssetCatalog.defaultItems
assetCatalog.effects.append(contentsOf: [customLUTFilter, customDuoToneFilter])
builder.assetCatalog = assetCatalog
let thumbnailURL = self.downloadLocations[thumbnailFilename]
let thumbnailData = try? Data(contentsOf: thumbnailURL!)
let thumbnail = UIImage(data: thumbnailData!)
let customFilterGroup = Group(identifier: "custom_filter_category", displayName: "Custom", thumbnail: thumbnail, memberIdentifiers: ["custom_lut_filter"])
builder.configureFilterToolController { options in
options.filterGroups.append(customFilterGroup)
}
}
let photoEditViewController = PhotoEditViewController(photoAsset: photo, configuration: configuration)
photoEditViewController.delegate = self
self.presentingViewController?.view.isUserInteractionEnabled = true
photoEditViewController.modalPresentationStyle = .fullScreen
self.presentingViewController?.present(photoEditViewController, animated: true, completion: nil)
}
}
}
presentingViewController?.view.isUserInteractionEnabled = false
downloadTask.resume()
}
}
private func removeResources() {
for location in downloadLocations.values {
try? FileManager.default.removeItem(at: location)
}
downloadLocations.removeAll()
}
func photoEditViewControllerShouldStart(_ photoEditViewController: PhotoEditViewController, task: PhotoEditorTask) -> Bool {
true
}
func photoEditViewControllerDidFinish(_ photoEditViewController: PhotoEditViewController, result: PhotoEditorResult) {
presentingViewController?.dismiss(animated: true, completion: {
self.removeResources()
})
}
func photoEditViewControllerDidFail(_ photoEditViewController: PhotoEditViewController, error: PhotoEditorError) {
print(error.localizedDescription)
presentingViewController?.dismiss(animated: true, completion: {
self.removeResources()
})
}
func photoEditViewControllerDidCancel(_ photoEditViewController: PhotoEditViewController) {
presentingViewController?.dismiss(animated: true, completion: {
self.removeResources()
})
}
}