Let's use Swift Package Manager (SwiftPM) ~ Creation ~

About SwiftPackageManager (hereinafter, SwiftPM), which can be used for iOS application development from Xcode 11 and has become considerably easier to use with Xcode 12 released the other day, this article is out of how to introduce it to the project and how to create your own library to SwiftPM It will be a method of creating your own library.

↓ Click here for introduction method

How to create a library

Installation environment

Start Swift PM

[Start Xcode] → [File] → [New] → [Swift Packages]

スクリーンショット 2020-10-12 9.35.17.png

[Project name] → [Create]

スクリーンショット 2020-10-12 9.36.05.png

Created screen This time it's up to the registration of my own library, so

I will only touch it. (If true, you should also write a test.)

スクリーンショット 2020-10-12 9.36.37.png

In Package.swift, describe the URL of the file you want to load or the external library. This time, load Nuke with an external library, extend UIImage, and load Nuke on the UIImage side.

Package.swift


// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "MyLibrary",
    platforms:[.iOS(.v11)],   //Platform to use (iOS 11 or above this time)
    products: [
        .library(
            name: "MyLibrary",
            targets: ["MyLibrary"]),
    ],
    dependencies: [
        .package(url: "https://github.com/kean/Nuke.git", from: "9.1.2")    //External library you want to load


    ],
    targets: [
depends on.
        .target(
            name: "MyLibrary",
            dependencies: [
                .product(name: "Nuke", package: "Nuke")    //Use the loaded library
            ]),
        .testTarget(
            name: "MyLibraryTests",
            dependencies: ["MyLibrary"]),
    ]
)

This completes the process of using the library.

MyLibrary.swift


import UIKit
import Nuke

public enum ProcessorsOption {
    case resize
    case resizeRound(radius: CGFloat)
    case resizeCircle
}

public typealias AspectMode = ImageProcessors.Resize.ContentMode

public extension UIImageView {
    
    func loadUrl(imageUrl: String?,
                 processorOption: ProcessorsOption = ProcessorsOption.resize,
                 aspectMode: AspectMode = .aspectFill,
                 crop: Bool = false,
                 placeHolder: UIImage? = nil,
                 failureImage: UIImage? = nil,
                 contentMode: UIView.ContentMode? = nil) {
        guard let url: String = imageUrl,
            let loadUrl: URL = URL(string: url) else {
            self.image = failureImage
            return
        }
        
        let resizeProcessor = ImageProcessors.Resize(size: self.bounds.size,
                                                     contentMode: aspectMode, crop: crop)
        let processors: [ImageProcessing]
        
        switch processorOption {
        case .resize:
            processors = [resizeProcessor]
        case .resizeRound(let radius):
            processors = [resizeProcessor, ImageProcessors.RoundedCorners(radius: radius)]
        case .resizeCircle:
            processors = [resizeProcessor, ImageProcessors.Circle()]
        }
        
        let request = ImageRequest(
            url: loadUrl,
            processors: processors
        )
        var contentModes: ImageLoadingOptions.ContentModes?
        
        if let mode = contentMode {
            contentModes = ImageLoadingOptions.ContentModes.init(success: mode,
                                                                 failure: mode, placeholder: mode)
        }
        let loadingOptions = ImageLoadingOptions(placeholder: placeHolder,
                                                 failureImage: failureImage, contentModes: contentModes)
        
        Nuke.loadImage(with: request, options: loadingOptions, into: self)
    }
}

I will omit the contents in the library. (I tried to use Extension as the one that loads and resizes Nuke images.)

As it is, ** No such module'UIKit' ** is displayed and it cannot be built.

[Tests] → [MyLibraryTests] → Change the contents of [MyLibraryTests.swift] スクリーンショット 2020-10-12 16.19.16.png

Changed build from "My Mac" to "Any Any iOS Device"

スクリーンショット 2020-10-12 16.20.09.png

If the build is successful, you're done.

Use the Git command or source tree to commit the last created one, add a tag, and push it to github and you're done. ↑ About this

Please refer to.

If you are thinking of distributing it as a whole, we recommend that you license it. How to license on github

Please refer to.

By the way, the one created this time is published on github. If you like, please use it with Star.

github https://github.com/isamiodagiri/ExtendedImageViewWithNuke

↓ Click here for the introduction method.

reference

Cooperation

How to register your own library in SwiftPM, you can not understand only how to introduce an external library by reference only, "** App Dojo Salon ** ”people also cooperated. Thank you very much.

Recommended Posts

Let's use Swift Package Manager (SwiftPM) ~ Creation ~
Let's use Swift Package Manager (SwiftPM) ~ Introduction ~
[Swift] Let's use extension
[Swift] Let's use Segmented Control
Use Swift Package on Playground
Let's use Swift Firebase Firebase Auth
Let's use jcmd