๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/UIKit

[iOS/Swift] Image Slide show + Firebase Storage ๋กœ ์Šฌ๋ผ์ด๋“œ ์ด๋ฏธ์ง€๋ทฐ ๊ตฌํ˜„ํ•˜๊ธฐ

by Danna 2021. 3. 28.
728x90
728x90

๐Ÿ–ผ ImageSlideshow

Customizable Swift image slideshow with circular scrolling, timer and full screen viewer

Image Slide show ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์—ฌ๋Ÿฌ ์ด๋ฏธ์ง€๋ฅผ ์˜†์œผ๋กœ ์Šฌ๋ผ์ด๋“œํ•˜๋Š” UI ๋ฅผ ๊ตฌํ˜„ํ•ด์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค! ์ธ์Šคํƒ€๊ทธ๋žจ์˜ ์ด๋ฏธ์ง€๊ฐ€ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒƒ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋‹ค์Œ์˜ ๊ธฐ๋Šฅ๋“ค์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์ด๋ฏธ์ง€ ์Šฌ๋ผ์ด๋“œ์‹œ ์›ํ˜• ๊ตฌ๋ถ„์ž(circular indicator) ์ด์šฉ 
  • ์ด๋ฏธ์ง€ ์Šฌ๋ผ์ด๋“œ์‹œ Label Indicator (ex 1/5) ์ œ๊ณต
  • ์ด๋ฏธ์ง€ ์Šฌ๋ผ์ด๋“œ๊ฐ€ ๊ฐ€๋Šฅํ•œ Full screen view ์ œ๊ณต
  • ์‹œ๊ฐ„์„ ์„ค์ •ํ•ด ์ž๋™์œผ๋กœ ์ด๋ฏธ์ง€ ์Šฌ๋ผ์ด๋“œ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋„๋ก ์ œ๊ณต
 

zvonicek/ImageSlideshow

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

github.com

 


๐Ÿ”ง Installation

์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ”„๋กœ์ ํŠธ์— ํฌํ•จ์„ ๋จผ์ € ์‹œ์ผœ์ค˜์•ผ๊ฒ ์ฃ ?! ์ €๋Š” CocoaPods ์„ ์ด์šฉํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—, [ pod init > Podfile ์ˆ˜์ • > pod install ] ์ดํ›„ ์ƒ์„ฑ๋œ Project.xcworkspace ํŒŒ์ผ์„ ์ด์šฉํ•ด ๊ฐœ๋ฐœํ–ˆ์Šต๋‹ˆ๋‹ค. Podfile ์— ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ํ•œ ์ค„ ์ถ”๊ฐ€ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

pod 'ImageSlideshow', '~> 1.9.0'

 

๐Ÿ”จ How to use

1. ImageSlideShow ํด๋ž˜์Šค์˜ View ์ƒ์„ฑํ•˜๊ธฐ

ImageSlideshow ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋จผ์ €, ImageSlideshow ํด๋ž˜์Šค์˜ View ๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Storyboar ๋ฅผ ์ด์šฉํ•œ๋‹ค๋ฉด UIView ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , Custom Class ๋ฅผ ImageSlideshow ๋กœ ์ง€์ •ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์Šคํ† ๋ฆฌ๋ณด๋“œ์— ์ปค์Šคํ…€ ํด๋ž˜์Šค ์„ค์ •ํ•˜๊ธฐ

 

2. ImageSlideShow ์— ์ด๋ฏธ์ง€ ์„ค์ •ํ•˜๊ธฐ

์ดํ›„, ImageSlideshow ์— ์ด๋ฏธ์ง€๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. UIImage ๋ฅผ ์ด์šฉํ•œ๋‹ค๋ฉด ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, AlamofireImage, SDWebImage, Kingfisher ์™€ ๊ฐ™์€ ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋ณ„๋„์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€ ์„ค์น˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. Github ๋ฌธ์„œ์—์„œ ์„ค์น˜ํ•ด์•ผํ•  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์–ด์š”.

 

์ง„ํ–‰์ค‘์ธ ํ”„๋กœ์ ํŠธ๋Š” Firebase Storage ์—์„œ ์ด๋ฏธ์ง€๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Firebase UI ์— ํฌํ•จ๋œ SDWebImage ๋ฅผ ์ด์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ด์šฉํ•˜๋Š” Github ๋ฌธ์„œ์— ์˜ˆ์ œ๊ฐ€ ์—†์–ด์„œ ๊ฑฑ์ •ํ–ˆ์ง€๋งŒ, ๋‹ค๋ฅธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ImageInputs.append(SDWebImageSource(url: url)) ์ฝ”๋“œ๋งŒ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ๋ผ์š”! 

ํ˜น์‹œ Firebase Storage ์™€ Image Slide show ๋ฅผ ํ•จ๊ป˜ ์ด์šฉํ•˜์‹œ๋Š” ๋ถ„๊ป˜ ๋„์›€์ด ๋ ๊นŒํ•ด์„œ ์ฝ”๋“œ ํ•จ๊ป˜ ์ฒจ๋ถ€ํ•ฉ๋‹ˆ๋‹ค. ๐Ÿ™‚
import ImageSlideshow

struct FirebaseStorage {

    static let baseURL = "https://firebasestorage.googleapis.com/v0/b/bucket-name/o/"
    static let query = "?alt=media"
    
    static func getImageInputs(images: [Image]) -> [SDWebImageSource] {
        var imageInputs: [SDWebImageSource] = []
        for image in images {
            let path = image.pictureUrl.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
            let url = URL(string: baseURL + path + query)!
            imageInputs.append(SDWebImageSource(url: url))
        }
        return imageInputs
    }
}

์œ„์˜ ํ•จ์ˆ˜๋ฅผ ImageSlideshow ๊ฐ€ ์žˆ๋Š” ViewController ์—์„œ ์ด์šฉํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค! ์ฝ”๋“œ์—์„œ ์ด์šฉํ•œ ImageSlideshow ์˜ ์ธ์ž๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

  • func setImageInputs(_ inputs: [InputSource]) :: ์ด๋ฏธ์ง€ ์„ค์ •
  • var contentScaleMode :: ์ด๋ฏธ์ง€ ์Šค์ผ€์ผ ์„ค์ • (UIView.ContentMode ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค)
  • var pageIndicatorPosition :: ํŽ˜์ด์ง€ ๊ตฌ๋ถ„์ž๊ฐ€ ๋‚˜ํƒ€๋‚˜๋Š” ์œ„์น˜ ์„ค์ •
class CustomViewController {
  // ...
  private func updateImageSlide(_ shopImages: [Image]) {
    // Firebase Storage ์ด๋ฏธ์ง€ ์—ฐ๋™
    let imageInputs = FirebaseStorage.getImageInputs(images: shopImages)
    shopImageSlide.setImageInputs(imageInputs)
    shopImageSlide.contentScaleMode = .scaleAspectFill
    shopImageSlide.pageIndicatorPosition = .init(horizontal: .center, vertical: .bottom)
  }
}

์ •์ƒ์ ์œผ๋กœ ๊ตฌํ˜„์‹œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์„ ๋งŒ๋‚˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๐Ÿคฉ ๊ตฌํ˜„์ค‘์ธ ์•ฑ์˜ ์ผ๋ถ€์ด๊ณ , ์‚ฌ์ง„์€ ๋ธŒ๋ฆฟ์ง€์—ฃ์ง€ ์นดํŽ˜์—์„œ ์ œ๊ณต๋ฐ›์•˜์Šต๋‹ˆ๋‹ค.

 

Image Slide show ์ ์šฉํ™”๋ฉด

 

๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ Label Indicator, Full screen view ์ ์šฉํ•˜๊ธฐ๋ฅผ ๋‹ค๋ค„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

[iOS/Swift] Image Slide show - Label Page Indicator, Full screen view ๊ตฌํ˜„ํ•˜๊ธฐ

๐Ÿ–ผ ImageSlideshow ImageSlideshow ์„ค์น˜, ๋ทฐ ์ƒ์„ฑํ•˜๊ธฐ, ์ด๋ฏธ์ง€ ๋“ฑ๋กํ•˜๋Š” ๋ฒ•์€ ์ด์ „ ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”. ํ•ด๋‹น ํฌ์ŠคํŒ…์—์„œ๋Š” Label Page Indicator ์ ์šฉํ•˜๋Š” ๋ฒ•, ์ด๋ฏธ์ง€ ํด๋ฆญ์‹œ ์ „์ฒด ํ™”๋ฉด์œผ๋กœ ์ „ํ™˜ํ•˜๋Š” ๋ฒ•์—

jellysong.tistory.com


์ฐธ๊ณ  ๋งํฌ

 

 

728x90
728x90