Skip to content

Commit

Permalink
修改ImagePicker库,使其可以支持处理中效果
Browse files Browse the repository at this point in the history
  • Loading branch information
W-Mai committed Oct 15, 2021
1 parent 5bf8b40 commit 78c837c
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 67 deletions.
111 changes: 111 additions & 0 deletions ImagePickerView/ImagePickerView.swift
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
}
81 changes: 81 additions & 0 deletions ImagePickerView/UIImagePickerView.swift
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
}
45 changes: 21 additions & 24 deletions xSticker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 52;
objectVersion = 50;
objects = {

/* Begin PBXBuildFile section */
Expand All @@ -28,11 +28,14 @@
5E47421827185E38001E0E11 /* ld.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 5E47421727185E38001E0E11 /* ld.jpg */; };
5E47421927185E38001E0E11 /* ld.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 5E47421727185E38001E0E11 /* ld.jpg */; };
5E8686E62718A51C00A65DF4 /* Persistence+Operate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E8686E52718A51C00A65DF4 /* Persistence+Operate.swift */; };
5E8686E92719717700A65DF4 /* ImagePickerView in Frameworks */ = {isa = PBXBuildFile; productRef = 5E8686E82719717700A65DF4 /* ImagePickerView */; };
5E8686EB2719764100A65DF4 /* StickerManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E8686EA2719764100A65DF4 /* StickerManager.swift */; };
5E8686EC2719764100A65DF4 /* StickerManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E8686EA2719764100A65DF4 /* StickerManager.swift */; };
5E8686F22719E5EA00A65DF4 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5E8686F12719E5EA00A65DF4 /* Assets.xcassets */; };
5E8686F32719E5EA00A65DF4 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5E8686F12719E5EA00A65DF4 /* Assets.xcassets */; };
5E8686F9271A052700A65DF4 /* ImagePickerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E8686F7271A052700A65DF4 /* ImagePickerView.swift */; };
5E8686FA271A052700A65DF4 /* ImagePickerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E8686F7271A052700A65DF4 /* ImagePickerView.swift */; };
5E8686FB271A052700A65DF4 /* UIImagePickerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E8686F8271A052700A65DF4 /* UIImagePickerView.swift */; };
5E8686FC271A052700A65DF4 /* UIImagePickerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E8686F8271A052700A65DF4 /* UIImagePickerView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -82,6 +85,8 @@
5E8686E52718A51C00A65DF4 /* Persistence+Operate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Persistence+Operate.swift"; sourceTree = "<group>"; };
5E8686EA2719764100A65DF4 /* StickerManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StickerManager.swift; sourceTree = "<group>"; };
5E8686F12719E5EA00A65DF4 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
5E8686F7271A052700A65DF4 /* ImagePickerView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImagePickerView.swift; sourceTree = "<group>"; };
5E8686F8271A052700A65DF4 /* UIImagePickerView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImagePickerView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -90,7 +95,6 @@
buildActionMask = 2147483647;
files = (
5E474211271848DD001E0E11 /* CloudKit.framework in Frameworks */,
5E8686E92719717700A65DF4 /* ImagePickerView in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -109,6 +113,7 @@
5E4741CE27181B24001E0E11 = {
isa = PBXGroup;
children = (
5E8686F6271A052700A65DF4 /* ImagePickerView */,
5E8686ED2719764B00A65DF4 /* Shared */,
5E4741D927181B24001E0E11 /* xSticker */,
5E4741F6271824CE001E0E11 /* iMsgExtPart */,
Expand Down Expand Up @@ -190,6 +195,15 @@
path = Shared;
sourceTree = "<group>";
};
5E8686F6271A052700A65DF4 /* ImagePickerView */ = {
isa = PBXGroup;
children = (
5E8686F7271A052700A65DF4 /* ImagePickerView.swift */,
5E8686F8271A052700A65DF4 /* UIImagePickerView.swift */,
);
path = ImagePickerView;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand All @@ -209,7 +223,6 @@
);
name = xSticker;
packageProductDependencies = (
5E8686E82719717700A65DF4 /* ImagePickerView */,
);
productName = xSticker;
productReference = 5E4741D727181B24001E0E11 /* xSticker.app */;
Expand Down Expand Up @@ -259,7 +272,6 @@
);
mainGroup = 5E4741CE27181B24001E0E11;
packageReferences = (
5E8686E72719717700A65DF4 /* XCRemoteSwiftPackageReference "ImagePickerView" */,
);
productRefGroup = 5E4741D827181B24001E0E11 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -303,8 +315,10 @@
files = (
5E47420A2718386F001E0E11 /* MessagesViewController.swift in Sources */,
5E4741E427181B25001E0E11 /* Persistence.swift in Sources */,
5E8686FB271A052700A65DF4 /* UIImagePickerView.swift in Sources */,
5E4741DD27181B24001E0E11 /* ContentView.swift in Sources */,
5E47420B27183878001E0E11 /* CustomStickerView.swift in Sources */,
5E8686F9271A052700A65DF4 /* ImagePickerView.swift in Sources */,
5E4741E727181B25001E0E11 /* xSticker.xcdatamodeld in Sources */,
5E8686E62718A51C00A65DF4 /* Persistence+Operate.swift in Sources */,
5E4741DB27181B24001E0E11 /* xStickerApp.swift in Sources */,
Expand All @@ -319,7 +333,9 @@
5E47420C271838F8001E0E11 /* Persistence.swift in Sources */,
5E4741F8271824CE001E0E11 /* MessagesViewController.swift in Sources */,
5E47420D2718392A001E0E11 /* xSticker.xcdatamodeld in Sources */,
5E8686FC271A052700A65DF4 /* UIImagePickerView.swift in Sources */,
5E8686EC2719764100A65DF4 /* StickerManager.swift in Sources */,
5E8686FA271A052700A65DF4 /* ImagePickerView.swift in Sources */,
5E474208271825AF001E0E11 /* CustomStickerView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -584,25 +600,6 @@
};
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
5E8686E72719717700A65DF4 /* XCRemoteSwiftPackageReference "ImagePickerView" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/rebeloper/ImagePickerView.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.2.2;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
5E8686E82719717700A65DF4 /* ImagePickerView */ = {
isa = XCSwiftPackageProductDependency;
package = 5E8686E72719717700A65DF4 /* XCRemoteSwiftPackageReference "ImagePickerView" */;
productName = ImagePickerView;
};
/* End XCSwiftPackageProductDependency section */

/* Begin XCVersionGroup section */
5E4741E527181B25001E0E11 /* xSticker.xcdatamodeld */ = {
isa = XCVersionGroup;
Expand Down

This file was deleted.

Loading

0 comments on commit 78c837c

Please sign in to comment.