SKPhotoBrowser/SKPhotoBrowserExample/SKPhotoBrowserExample/FromWebViewController.swift

85 lines
2.3 KiB
Swift

//
// FromWebViewController.swift
// SKPhotoBrowserExample
//
// Created by suzuki_keishi on 2015/10/06.
// Copyright © 2015 suzuki_keishi. All rights reserved.
//
import UIKit
import SKPhotoBrowser
import SDWebImage
class FromWebViewController: UIViewController, SKPhotoBrowserDelegate {
@IBOutlet weak var imageView: UIImageView!
var images = [SKPhotoProtocol]()
override func viewDidLoad() {
super.viewDidLoad()
SKCache.sharedCache.imageCache = CustomImageCache()
imageView.sd_setImage(with: URL(string: "https://placehold.jp/1500x1500.png")) {
guard let url = $0.3?.absoluteString else { return }
SKCache.sharedCache.setImage($0.0!, forKey: url)
}
}
@IBAction func pushButton(_ sender: AnyObject) {
let browser = SKPhotoBrowser(photos: createWebPhotos())
browser.initializePageIndex(0)
browser.delegate = self
present(browser, animated: true, completion: nil)
}
}
// MARK: - SKPhotoBrowserDelegate
extension FromWebViewController {
func didDismissAtPageIndex(_ index: Int) {
}
func didDismissActionSheetWithButtonIndex(_ buttonIndex: Int, photoIndex: Int) {
}
func removePhoto(_ browser: SKPhotoBrowser, index: Int, reload: (() -> Void)) {
SKCache.sharedCache.removeImageForKey("somekey")
reload()
}
}
// MARK: - private
private extension FromWebViewController {
func createWebPhotos() -> [SKPhotoProtocol] {
return (0..<10).map { (i: Int) -> SKPhotoProtocol in
let photo = SKPhoto.photoWithImageURL("https://placehold.jp/150\(i)x150\(i).png")
photo.caption = caption[i%10]
photo.shouldCachePhotoURLImage = true
return photo
}
}
}
class CustomImageCache: SKImageCacheable {
var cache: SDImageCache
init() {
let cache = SDImageCache(namespace: "com.suzuki.custom.cache")
self.cache = cache!
}
func imageForKey(_ key: String) -> UIImage? {
guard let image = cache.imageFromDiskCache(forKey: key) else { return nil }
return image
}
func setImage(_ image: UIImage, forKey key: String) {
cache.store(image, forKey: key)
}
func removeImageForKey(_ key: String) {
}
}