적당한 고통은 희열이다

- 댄 브라운 '다빈치 코드' 중에서

Swift iOS 앱 개발/iOS

[Swift iOS] imageSlideShow

hongssup_ 2022. 9. 20. 16:09
반응형

이미지 슬라이드 쇼를 간편하게 적용할 수 있는 ImageSlideShow 라이브러리를 사용해보았다. 

https://github.com/zvonicek/ImageSlideshow

 

GitHub - zvonicek/ImageSlideshow: Swift image slideshow with circular scrolling, timer and full screen viewer

Swift image slideshow with circular scrolling, timer and full screen viewer - GitHub - zvonicek/ImageSlideshow: Swift image slideshow with circular scrolling, timer and full screen viewer

github.com

pod 'ImageSlideshow'

install 해주고

 

import ImageSlideshow

(이미지 소스 세팅하기 위해서는 다른 라이브러리도 함께 사용해주야함..? 나는 KingFisher 사용했다)

let bannerSlide = ImageSlideshow()

lazy var labelIndicatorBgView : UIView = {
    let view = UIView()
    view.backgroundColor = UIColor(white: 0.2, alpha: 0.3)
    view.layer.cornerRadius = 10
    return view
}()
bannerSlide.contentScaleMode = .scaleAspectFill
bannerSlide.slideshowInterval = 3
bannerSlide.pageIndicatorPosition = .init(horizontal: .right(padding: 20), vertical: .customBottom(padding: 20))

let labelIndicator = LabelPageIndicator()
labelIndicator.numberOfPages = images.count
labelIndicator.textColor = .white
labelIndicator.font = UIFont.systemFont(ofSize: 12, weight: .medium)

bannerSlide.pageIndicator = labelIndicator

bannerSlide.addSubview(labelIndicatorBgView)
labelIndicatorBgView.snp.makeConstraints {
    $0.centerX.centerY.equalTo(labelIndicator)
    $0.width.equalTo(labelIndicator).multipliedBy(1.6)
    $0.height.equalTo(labelIndicator).multipliedBy(1.4)
}
self.bannerSlide.bringSubviewToFront(labelIndicator)

let sources = images.map {
    KFSource(urlString: $0)!
}
bannerSlide.setImageInputs(sources)

 

더보기

다음과 같이 설정해주면, 유효한 url이 아닐 경우 아예 source로 추가가 안될 줄 알았는데 그건 아니더라..? 

유효한 이미지 url이 아니면 아예 배너 리스트에서 안보이게 하고싶었는데.. 

var sources: [KFSource] = []
for image in images {
    if let url = KFSource(urlString: image) {
        sources.append(url)
    }
}

 

소스 이미지 setting해줄 때

예제에서 보면 KingFisherSource를 그냥 라이브러리 추가만 하면 사용할 수 있던데 왜 난 안되는거지? 

다음 코드를 따로 추가해줘서 사용을 했다. 

더보기
import Kingfisher
import ImageSlideshow
/// Input Source to image using Kingfisher
public class KFSource: NSObject, InputSource {
    /// url to load
    public var url: URL

    /// placeholder used before image is loaded
    public var placeholder: UIImage?

    /// options for displaying, ie. [.transition(.fade(0.2))]
    public var options: KingfisherOptionsInfo?

    /// Initializes a new source with a URL
    /// - parameter url: a url to be loaded
    /// - parameter placeholder: a placeholder used before image is loaded
    /// - parameter options: options for displaying
    public init(url: URL, placeholder: UIImage? = nil, options: KingfisherOptionsInfo? = nil) {
        self.url = url
        self.placeholder = placeholder
        self.options = options
        super.init()
    }

    /// Initializes a new source with a URL string
    /// - parameter urlString: a string url to load
    /// - parameter placeholder: a placeholder used before image is loaded
    /// - parameter options: options for displaying
    public init?(urlString: String, placeholder: UIImage? = nil, options: KingfisherOptionsInfo? = nil) {
        if let validUrl = URL(string: urlString) {
            self.url = validUrl
            self.placeholder = placeholder
            self.options = options
            super.init()
        } else {
            return nil
        }
    }

    /// Load an image to an UIImageView
    ///
    /// - Parameters:
    ///   - imageView: UIImageView that receives the loaded image
    ///   - callback: Completion callback with an optional image
    @objc
    public func load(to imageView: UIImageView, with callback: @escaping (UIImage?) -> Void) {
        imageView.kf.setImage(with: self.url, placeholder: self.placeholder, options: self.options, progressBlock: nil) { result in
            switch result {
            case .success(let image):
                callback(image.image)
            case .failure:
                callback(nil)
            }
        }
    }

    /// Cancel an image download task
    ///
    /// - Parameter imageView: UIImage view with the download task that should be canceled
    public func cancelLoad(on imageView: UIImageView) {
        imageView.kf.cancelDownloadTask()
    }
}

참고 : https://github.com/zvonicek/ImageSlideshow/blob/master/ImageSlideshow/Classes/InputSources/KingfisherSource.swift

 

GitHub - zvonicek/ImageSlideshow: Swift image slideshow with circular scrolling, timer and full screen viewer

Swift image slideshow with circular scrolling, timer and full screen viewer - GitHub - zvonicek/ImageSlideshow: Swift image slideshow with circular scrolling, timer and full screen viewer

github.com

 

 

 


참고 : 개발하는 정대리

728x90
반응형