-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
256 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// | ||
// ImagePickerView.swift | ||
// | ||
// | ||
// Created by Alex Nagy on 19.01.2021. | ||
// | ||
|
||
import SwiftUI | ||
import UIKit | ||
import PhotosUI | ||
|
||
public struct ImagePickerView: UIViewControllerRepresentable { | ||
|
||
public typealias UIViewControllerType = PHPickerViewController | ||
|
||
public init(filter: PHPickerFilter = .images, selectionLimit: Int = 1, delegate: PHPickerViewControllerDelegate) { | ||
self.filter = filter | ||
self.selectionLimit = selectionLimit | ||
self.delegate = delegate | ||
} | ||
|
||
private let filter: PHPickerFilter | ||
private let selectionLimit: Int | ||
private let delegate: PHPickerViewControllerDelegate | ||
|
||
public func makeUIViewController(context: Context) -> PHPickerViewController { | ||
var configuration = PHPickerConfiguration(photoLibrary: PHPhotoLibrary.shared()) | ||
configuration.filter = filter | ||
configuration.selectionLimit = selectionLimit | ||
|
||
let controller = PHPickerViewController(configuration: configuration) | ||
controller.delegate = delegate | ||
return controller | ||
} | ||
|
||
public func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) { } | ||
} | ||
|
||
extension ImagePickerView { | ||
public class Delegate: NSObject, PHPickerViewControllerDelegate { | ||
|
||
public init(isPresented: Binding<Bool>, isProccesing: Binding<Bool>,didCancel: @escaping (PHPickerViewController) -> (), didSelect: @escaping (ImagePickerResult) -> (), didFail: @escaping (ImagePickerError) -> ()) { | ||
self._isPresented = isPresented | ||
self._isProcessing = isProccesing | ||
self.didCancel = didCancel | ||
self.didSelect = didSelect | ||
self.didFail = didFail | ||
} | ||
|
||
@Binding var isPresented: Bool | ||
@Binding var isProcessing: Bool | ||
private let didCancel: (PHPickerViewController) -> () | ||
private let didSelect: (ImagePickerResult) -> () | ||
private let didFail: (ImagePickerError) -> () | ||
|
||
public func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { | ||
isProcessing = true | ||
if results.count == 0 { | ||
self.isPresented = false | ||
self.isProcessing = false | ||
self.didCancel(picker) | ||
return | ||
} | ||
var images = [UIImage]() | ||
for i in 0..<results.count { | ||
let result = results[i] | ||
if result.itemProvider.canLoadObject(ofClass: UIImage.self) { | ||
result.itemProvider.loadObject(ofClass: UIImage.self) { newImage, error in | ||
if let error = error { | ||
self.isPresented = false | ||
self.isProcessing = false | ||
self.didFail(ImagePickerError(picker: picker, error: error)) | ||
} else if let image = newImage as? UIImage { | ||
images.append(image) | ||
} | ||
if images.count == results.count { | ||
self.isPresented = false | ||
self.isProcessing = false | ||
if images.count != 0 { | ||
self.didSelect(ImagePickerResult(picker: picker, images: images)) | ||
} else { | ||
self.didCancel(picker) | ||
} | ||
} | ||
} | ||
} else { | ||
self.isPresented = false | ||
self.isProcessing = false | ||
self.didFail(ImagePickerError(picker: picker, error: ImagePickerViewError.cannotLoadObject)) | ||
} | ||
} | ||
|
||
|
||
} | ||
} | ||
} | ||
|
||
public struct ImagePickerResult { | ||
public let picker: PHPickerViewController | ||
public let images: [UIImage] | ||
} | ||
|
||
public struct ImagePickerError { | ||
public let picker: PHPickerViewController | ||
public let error: Error | ||
} | ||
|
||
public enum ImagePickerViewError: Error { | ||
case cannotLoadObject | ||
case failedToLoadObject | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// | ||
// UIImagePickerView.swift | ||
// | ||
// | ||
// Created by Alex Nagy on 28.02.2021. | ||
// | ||
|
||
import SwiftUI | ||
import UIKit | ||
|
||
@available(iOS 13.0, *) | ||
public struct UIImagePickerView: UIViewControllerRepresentable { | ||
|
||
public typealias UIViewControllerType = UIImagePickerController | ||
|
||
/// Image Picker with UIImagePickerController | ||
/// - Parameters: | ||
/// - allowsEditing: does it allow editing | ||
/// - sourceType: source | ||
/// - delegate: Image Picker Delegate | ||
public init(allowsEditing: Bool = true, | ||
sourceType: UIImagePickerController.SourceType = .photoLibrary, | ||
delegate: UIImagePickerControllerDelegate & UINavigationControllerDelegate) { | ||
self.allowsEditing = allowsEditing | ||
self.sourceType = sourceType | ||
self.delegate = delegate | ||
} | ||
|
||
private let allowsEditing: Bool | ||
private let sourceType: UIImagePickerController.SourceType | ||
private let delegate: UIImagePickerControllerDelegate & UINavigationControllerDelegate | ||
|
||
public func makeUIViewController(context: UIViewControllerRepresentableContext<UIImagePickerView>) -> UIImagePickerController { | ||
let controller = UIImagePickerController() | ||
controller.allowsEditing = allowsEditing | ||
controller.sourceType = sourceType | ||
controller.delegate = delegate | ||
return controller | ||
} | ||
|
||
public func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<UIImagePickerView>) { } | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
extension UIImagePickerView { | ||
|
||
public class Delegate: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate { | ||
|
||
public init(isPresented: Binding<Bool>, didCancel: @escaping (UIImagePickerController) -> (), didSelect: @escaping (UIImagePickerResult) -> ()) { | ||
self._isPresented = isPresented | ||
self.didCancel = didCancel | ||
self.didSelect = didSelect | ||
} | ||
|
||
@Binding var isPresented: Bool | ||
private let didCancel: (UIImagePickerController) -> () | ||
private let didSelect: (UIImagePickerResult) -> () | ||
|
||
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { | ||
var image = UIImage() | ||
if let editedImage = info[.editedImage] as? UIImage { | ||
image = editedImage | ||
} else if let originalImage = info[.originalImage] as? UIImage { | ||
image = originalImage | ||
} | ||
isPresented = false | ||
didSelect(UIImagePickerResult(picker: picker, image: image)) | ||
} | ||
|
||
public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { | ||
isPresented = false | ||
didCancel(picker) | ||
} | ||
} | ||
|
||
} | ||
|
||
public struct UIImagePickerResult { | ||
public let picker: UIImagePickerController | ||
public let image: UIImage | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
xSticker.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.