├── .gitattributes ├── .gitignore ├── .swiftpm └── xcode │ └── package.xcworkspace │ └── contents.xcworkspacedata ├── LICENSE ├── Package.swift ├── README.md ├── Sources ├── DocumentPicker │ └── PickerView+Extension.swift └── ImagePickerView │ └── ImagePickerView.swift └── Tests └── ImagePickerViewTests └── ImagePickerViewTests.swift /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Uzay Altıner 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.7 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | 4 | import PackageDescription 5 | 6 | let package = Package( 7 | name: "ImagePickerView", 8 | products: [ 9 | // Products define the executables and libraries a package produces, and make them visible to other packages. 10 | .library( 11 | name: "ImagePickerView", 12 | targets: ["ImagePickerView"]), 13 | ], 14 | dependencies: [ 15 | // Dependencies declare other packages that this package depends on. 16 | // .package(url: /* package url */, from: "1.0.0"), 17 | ], 18 | targets: [ 19 | // Targets are the basic building blocks of a package. A target can define a module or a test suite. 20 | // Targets can depend on other targets in this package, and on products in packages this package depends on. 21 | .target( 22 | name: "ImagePickerView", 23 | dependencies: []), 24 | .testTarget( 25 | name: "ImagePickerViewTests", 26 | dependencies: ["ImagePickerView"]), 27 | ] 28 | ) 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # PickerView 4 | ImagePickerView allows you to combine UIImageView and UIImagePicker in one line of code. Also adds Document Picker with single line of code to any UIView. Ready to use in Storyboard or Programmatic coding. 5 | ## Installation 6 | 7 | **Swift Package Manager** 8 | 9 | In Xcode go to `File` -> `Add Packages` and insert uArl: 10 | 11 | ```https://github.com/uzayaltiner/ImagePickerView``` 12 | 13 | # Quick Start 14 | ## ImagePickerView 15 | ```import ImagePickerView``` 16 | 17 | **Usage** 18 | 19 | ```swift 20 | let imageView = ImagePickerView() 21 | view.addSubview(imageView) 22 | ``` 23 | Thats all! 24 | 25 | **Properties** (more to come) 26 | ```swift 27 | imageView.placeholder = UIImage(named: "your_asset") 28 | imageView.color = .yourColor 29 | imageView.cornerRadius = 10 30 | ``` 31 | 32 | ```swift 33 | imageView.onImagePicker = { selectedImage in 34 | // After selecting the image, you can perform the operations you want to do here. 35 | } 36 | ``` 37 | ## DocumentPicker 38 | 39 | **Usage** 40 | ```swift 41 | let exampleView = UIView() 42 | exampleView.setupDocumentPicker { url in 43 | // After selecting the document's url path, you can perform the operations you want to do here. 44 | } 45 | view.addSubview(exampleView) 46 | ``` 47 | 48 | 49 | ## Contribute 50 | 51 | I made this to improve myself and we can add more features for expand the library. If you want to help, feel free to create PR. 52 | 53 | ## Contact 54 | 55 | For your requests and suggestions, you can contact with me from uzayaltiner@gmail.com 56 | 57 | ## License 58 | 59 | [MIT](https://github.com/uzayaltiner/ImagePickerView/blob/main/LICENSE) 60 | -------------------------------------------------------------------------------- /Sources/DocumentPicker/PickerView+Extension.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DocumentPickerViewExtension.swift 3 | // 4 | // 5 | // Created by Mert Çetin on 31.03.2023. 6 | // 7 | 8 | import Foundation 9 | import MobileCoreServices 10 | import UIKit 11 | 12 | public extension UIView { 13 | private enum AssociatedKeys { 14 | static var onDocumentPicked: UInt8 = 0 15 | } 16 | 17 | private var onDocumentPicked: ((URL) -> Void)? { 18 | get { 19 | return objc_getAssociatedObject(self, &AssociatedKeys.onDocumentPicked) as? (URL) -> Void 20 | } 21 | set { 22 | objc_setAssociatedObject(self, &AssociatedKeys.onDocumentPicked, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) 23 | } 24 | } 25 | 26 | 27 | @objc func viewTappedForDocumentPicker(_ sender: UITapGestureRecognizer) { 28 | if let viewController = sender.view?.findViewController() { 29 | showDocumentPicker(from: viewController) 30 | } 31 | } 32 | 33 | public func setupDocumentPicker(onDocumentPicked: @escaping (URL) -> Void) { 34 | self.onDocumentPicked = onDocumentPicked 35 | let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(viewTappedForDocumentPicker(_:))) 36 | isUserInteractionEnabled = true 37 | addGestureRecognizer(tapGestureRecognizer) 38 | } 39 | 40 | public func showDocumentPicker(from viewController: UIViewController) { 41 | let documentPickerController = UIDocumentPickerViewController(documentTypes: [kUTTypePDF as String], in: .import) 42 | documentPickerController.delegate = self 43 | 44 | DispatchQueue.main.async { 45 | viewController.present(documentPickerController, animated: true, completion: nil) 46 | } 47 | } 48 | } 49 | 50 | public extension UIView: UIDocumentPickerDelegate { 51 | public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) { 52 | if let selectedURL = urls.first { 53 | onDocumentPicked?(selectedURL) 54 | } 55 | controller.dismiss(animated: true, completion: nil) 56 | } 57 | 58 | public func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) { 59 | controller.dismiss(animated: true, completion: nil) 60 | } 61 | } 62 | 63 | public extension UIView { 64 | func findViewController() -> UIViewController? { 65 | if let nextResponder = next as? UIViewController { 66 | return nextResponder 67 | } else if let nextResponder = next as? UIView { 68 | return nextResponder.findViewController() 69 | } else { 70 | return nil 71 | } 72 | } 73 | } 74 | 75 | -------------------------------------------------------------------------------- /Sources/ImagePickerView/ImagePickerView.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | import UIKit 3 | 4 | public final class ImagePickerView: UIImageView { 5 | // MARK: - Properties - 6 | 7 | public var placeholderImage: UIImage? { 8 | didSet { 9 | image = placeholderImage 10 | } 11 | } 12 | 13 | public var color: UIColor? { 14 | didSet { 15 | backgroundColor = color 16 | } 17 | } 18 | 19 | public var cornerRadius: CGFloat? { 20 | didSet { 21 | layer.cornerRadius = cornerRadius ?? 0 22 | } 23 | } 24 | 25 | /// Determines what happens after the image is selected and returns a UIImage. 26 | public var onImagePicked: ((UIImage) -> Void)? 27 | public init() { 28 | super.init(frame: .zero) 29 | setupTapGestureRecognizer() 30 | } 31 | 32 | @available(*, unavailable) 33 | required init?(coder: NSCoder) { 34 | fatalError("init(coder:) has not been implemented") 35 | } 36 | 37 | override public func awakeFromNib() { 38 | super.awakeFromNib() 39 | setupTapGestureRecognizer() 40 | } 41 | 42 | private func setupTapGestureRecognizer() { 43 | let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageViewTapped)) 44 | isUserInteractionEnabled = true 45 | addGestureRecognizer(tapGestureRecognizer) 46 | } 47 | 48 | public func showImagePicker() { 49 | let imagePickerController = UIImagePickerController() 50 | imagePickerController.delegate = self 51 | imagePickerController.sourceType = .photoLibrary 52 | 53 | if let topViewController = findViewController() { 54 | DispatchQueue.main.async { 55 | topViewController.present(imagePickerController, animated: true, completion: nil) 56 | } 57 | } 58 | } 59 | 60 | @objc public func imageViewTapped() { 61 | showImagePicker() 62 | } 63 | } 64 | 65 | extension ImagePickerView: UIImagePickerControllerDelegate, UINavigationControllerDelegate { 66 | public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) { 67 | if let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage { 68 | image = selectedImage 69 | onImagePicked?(selectedImage) 70 | } 71 | picker.dismiss(animated: true, completion: nil) 72 | } 73 | 74 | public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 75 | picker.dismiss(animated: true, completion: nil) 76 | } 77 | } 78 | 79 | private extension UIView { 80 | func findViewController() -> UIViewController? { 81 | if let nextResponder = next as? UIViewController { 82 | return nextResponder 83 | } else if let nextResponder = next as? UIView { 84 | return nextResponder.findViewController() 85 | } else { 86 | return nil 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /Tests/ImagePickerViewTests/ImagePickerViewTests.swift: -------------------------------------------------------------------------------- 1 | @testable import ImagePickerView 2 | import XCTest 3 | 4 | final class ImagePickerViewTests: XCTestCase { 5 | var imagePickerView: ImagePickerView! 6 | 7 | override func setUp() { 8 | super.setUp() 9 | imagePickerView = ImagePickerView() 10 | } 11 | 12 | override func tearDown() { 13 | imagePickerView = nil 14 | super.tearDown() 15 | } 16 | 17 | func testPlaceholderImage() { 18 | let placeholderImage = UIImage(named: "test_image") 19 | imagePickerView.placeholderImage = placeholderImage 20 | XCTAssertEqual(imagePickerView.image, placeholderImage) 21 | } 22 | 23 | func testColor() { 24 | let testColor = UIColor.red 25 | imagePickerView.color = testColor 26 | XCTAssertEqual(imagePickerView.backgroundColor, testColor) 27 | } 28 | 29 | func testCornerRadius() { 30 | let testCornerRadius: CGFloat = 10.0 31 | imagePickerView.cornerRadius = testCornerRadius 32 | XCTAssertEqual(imagePickerView.layer.cornerRadius, testCornerRadius) 33 | } 34 | } 35 | --------------------------------------------------------------------------------