Skip to content

Commit 890be5f

Browse files
authored
Additional example (#74)
update sample project
1 parent 6b6973a commit 890be5f

10 files changed

+319
-47
lines changed

Example/Demo.xcodeproj/project.pbxproj

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
A68EAF90239634A000158120 /* ExampleListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A68EAF8F239634A000158120 /* ExampleListViewController.swift */; };
11+
A68EAF922396361C00158120 /* BasicViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A68EAF912396361C00158120 /* BasicViewController.swift */; };
12+
A68EAF9423963A9100158120 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A68EAF9323963A9100158120 /* Extensions.swift */; };
13+
A68EAF9623963B3600158120 /* WithURLViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A68EAF9523963B3600158120 /* WithURLViewController.swift */; };
14+
A6AB18E4239752C800BF0681 /* WithURLsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6AB18E3239752C800BF0681 /* WithURLsViewController.swift */; };
1015
A6BC0BC8239139F8004A4E46 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6BC0BC7239139F8004A4E46 /* AppDelegate.swift */; };
1116
A6BC0BD1239139F9004A4E46 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A6BC0BD0239139F9004A4E46 /* Assets.xcassets */; };
1217
A6BC0BD4239139F9004A4E46 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A6BC0BD2239139F9004A4E46 /* LaunchScreen.storyboard */; };
@@ -20,6 +25,11 @@
2025
/* Begin PBXFileReference section */
2126
10710A5B07A80798DED8269E /* Pods_Demo.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Demo.framework; sourceTree = BUILT_PRODUCTS_DIR; };
2227
20E424F2DB4A27D25989365C /* Pods-Demo.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Demo.release.xcconfig"; path = "Target Support Files/Pods-Demo/Pods-Demo.release.xcconfig"; sourceTree = "<group>"; };
28+
A68EAF8F239634A000158120 /* ExampleListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleListViewController.swift; sourceTree = "<group>"; };
29+
A68EAF912396361C00158120 /* BasicViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BasicViewController.swift; sourceTree = "<group>"; };
30+
A68EAF9323963A9100158120 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
31+
A68EAF9523963B3600158120 /* WithURLViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WithURLViewController.swift; sourceTree = "<group>"; };
32+
A6AB18E3239752C800BF0681 /* WithURLsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WithURLsViewController.swift; sourceTree = "<group>"; };
2333
A6BC0BC4239139F8004A4E46 /* Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Demo.app; sourceTree = BUILT_PRODUCTS_DIR; };
2434
A6BC0BC7239139F8004A4E46 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
2535
A6BC0BD0239139F9004A4E46 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
@@ -78,9 +88,14 @@
7888
A6BC0BD2239139F9004A4E46 /* LaunchScreen.storyboard */,
7989
A6BC0BD5239139F9004A4E46 /* Info.plist */,
8090
A6BC0BDD23913C30004A4E46 /* GalleryFlowLayout.swift */,
81-
A6BC0BDF23913C66004A4E46 /* GalleryViewController.swift */,
8291
A6BC0BE123914239004A4E46 /* ThumbCell.swift */,
8392
A6BC0BE723915420004A4E46 /* Data.swift */,
93+
A68EAF9323963A9100158120 /* Extensions.swift */,
94+
A68EAF8F239634A000158120 /* ExampleListViewController.swift */,
95+
A68EAF912396361C00158120 /* BasicViewController.swift */,
96+
A68EAF9523963B3600158120 /* WithURLViewController.swift */,
97+
A6BC0BDF23913C66004A4E46 /* GalleryViewController.swift */,
98+
A6AB18E3239752C800BF0681 /* WithURLsViewController.swift */,
8499
);
85100
path = Demo;
86101
sourceTree = "<group>";
@@ -208,11 +223,16 @@
208223
isa = PBXSourcesBuildPhase;
209224
buildActionMask = 2147483647;
210225
files = (
226+
A68EAF9623963B3600158120 /* WithURLViewController.swift in Sources */,
211227
A6BC0BE023913C66004A4E46 /* GalleryViewController.swift in Sources */,
228+
A6AB18E4239752C800BF0681 /* WithURLsViewController.swift in Sources */,
212229
A6BC0BE823915420004A4E46 /* Data.swift in Sources */,
230+
A68EAF9423963A9100158120 /* Extensions.swift in Sources */,
213231
A6BC0BDE23913C30004A4E46 /* GalleryFlowLayout.swift in Sources */,
232+
A68EAF90239634A000158120 /* ExampleListViewController.swift in Sources */,
214233
A6BC0BE223914239004A4E46 /* ThumbCell.swift in Sources */,
215234
A6BC0BC8239139F8004A4E46 /* AppDelegate.swift in Sources */,
235+
A68EAF922396361C00158120 /* BasicViewController.swift in Sources */,
216236
);
217237
runOnlyForDeploymentPostprocessing = 0;
218238
};

Example/Demo/AppDelegate.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
//
2-
// AppDelegate.swift
3-
// Demo
4-
//
5-
// Created by Michael Henry Pantaleon on 2019/11/29.
6-
// Copyright © 2019 Michael Henry Pantaleon. All rights reserved.
7-
//
8-
91
import UIKit
102

113
@UIApplicationMain
@@ -16,8 +8,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
168
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
179
window = UIWindow(frame: UIScreen.main.bounds)
1810

19-
let galleryVC = GalleryViewController()
20-
window?.rootViewController = UINavigationController(rootViewController: galleryVC)
11+
let exampleListVC = ExampleListViewController()
12+
window?.rootViewController = UINavigationController(rootViewController: exampleListVC)
2113
window?.makeKeyAndVisible()
2214
return true
2315
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import UIKit
2+
import MHFacebookImageViewer
3+
4+
class BasicViewController:UIViewController {
5+
6+
lazy var imageView:UIImageView = {
7+
let iv = UIImageView()
8+
iv.image = Data.images[0].resize(targetSize: .thumbnail)
9+
10+
// Setup Image Viewer
11+
iv.setupImageViewer()
12+
return iv
13+
}()
14+
15+
override func loadView() {
16+
super.loadView()
17+
view = UIView()
18+
view.backgroundColor = .white
19+
view.addSubview(imageView)
20+
imageView.translatesAutoresizingMaskIntoConstraints = false
21+
imageView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor, constant: 20).isActive = true
22+
imageView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20).isActive = true
23+
imageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
24+
imageView.heightAnchor.constraint(equalToConstant: 100).isActive = true
25+
}
26+
27+
override func viewDidLoad() {
28+
super.viewDidLoad()
29+
30+
}
31+
}

Example/Demo/Data.swift

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,17 @@
11
import UIKit
22

33
struct Data {
4-
static let images:[UIImage] = [
5-
UIImage(named: "cat1")!,
6-
UIImage(named: "cat2")!,
7-
UIImage(named: "cat3")!,
8-
UIImage(named: "cat4")!,
9-
UIImage(named: "cat5")!,
10-
UIImage(named: "cat1")!,
11-
UIImage(named: "cat2")!,
12-
UIImage(named: "cat3")!,
13-
UIImage(named: "cat4")!,
14-
UIImage(named: "cat5")!,
15-
UIImage(named: "cat1")!,
16-
UIImage(named: "cat2")!,
17-
UIImage(named: "cat3")!,
18-
UIImage(named: "cat4")!,
19-
UIImage(named: "cat5")!,
20-
UIImage(named: "cat1")!,
21-
UIImage(named: "cat2")!,
22-
UIImage(named: "cat3")!,
23-
UIImage(named: "cat4")!,
24-
UIImage(named: "cat5")!,
25-
UIImage(named: "cat1")!,
26-
UIImage(named: "cat2")!,
27-
UIImage(named: "cat3")!,
28-
UIImage(named: "cat4")!,
29-
UIImage(named: "cat5")!,
30-
UIImage(named: "cat1")!,
31-
UIImage(named: "cat2")!,
32-
UIImage(named: "cat3")!,
33-
UIImage(named: "cat4")!,
34-
UIImage(named: "cat5")!,
4+
5+
static let imageNames:[String] = [
6+
"cat1",
7+
"cat2",
8+
"cat3",
9+
"cat4",
10+
"cat5"
3511
]
12+
13+
static let images:[UIImage] = Self.imageNames.compactMap { UIImage(named: $0)! }
14+
15+
static let imageUrls:[URL] = Self.imageNames.compactMap {
16+
URL(string: "https://raw.githubusercontent.com/michaelhenry/MHFacebookImageViewer/master/Example/Demo/Assets.xcassets/\($0).imageset/\($0).jpg")! }
3617
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import UIKit
2+
3+
enum ExampleType:CaseIterable, CustomStringConvertible {
4+
5+
case basic
6+
case withURL
7+
case withUIImages
8+
case withURLs
9+
10+
var description: String {
11+
switch self {
12+
case .basic:
13+
return "Basic"
14+
case .withURL:
15+
return "With URL"
16+
case .withUIImages:
17+
return "With [UIImage]"
18+
case .withURLs:
19+
return "With [URL]"
20+
}
21+
}
22+
23+
var viewController:UIViewController {
24+
switch self {
25+
case .basic:
26+
return BasicViewController()
27+
case .withURL:
28+
return WithURLViewController()
29+
case .withUIImages:
30+
return WithImagesViewController()
31+
case .withURLs:
32+
return WithURLsViewController()
33+
}
34+
}
35+
}
36+
37+
class ExampleListViewController:UITableViewController {
38+
39+
var items:[ExampleType] = ExampleType.allCases
40+
41+
override func numberOfSections(in tableView: UITableView) -> Int {
42+
return 1
43+
}
44+
45+
override func tableView(
46+
_ tableView: UITableView, numberOfRowsInSection section: Int)
47+
-> Int {
48+
return items.count
49+
}
50+
51+
override func tableView(
52+
_ tableView: UITableView,
53+
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
54+
55+
let itemReuseId = "item_reuse_identifier"
56+
var cell = tableView.dequeueReusableCell(withIdentifier: itemReuseId)
57+
if cell == nil {
58+
cell = UITableViewCell(style: .default, reuseIdentifier: itemReuseId)
59+
}
60+
cell?.textLabel?.text = items[indexPath.row].description
61+
return cell!
62+
}
63+
64+
65+
override func tableView(
66+
_ tableView: UITableView,
67+
didSelectRowAt indexPath: IndexPath) {
68+
69+
navigationController?.pushViewController(
70+
items[indexPath.row].viewController, animated: true)
71+
}
72+
}

Example/Demo/Extensions.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import UIKit
2+
3+
extension UIImage {
4+
func resize(targetSize: CGSize) -> UIImage {
5+
6+
let widthRatio = targetSize.width / size.width
7+
let heightRatio = targetSize.height / size.height
8+
9+
var newSize: CGSize
10+
if widthRatio > heightRatio {
11+
newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
12+
} else {
13+
newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
14+
}
15+
16+
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
17+
18+
UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
19+
draw(in: rect)
20+
let newImage = UIGraphicsGetImageFromCurrentImageContext()
21+
UIGraphicsEndImageContext()
22+
23+
return newImage!
24+
}
25+
26+
func scale(by scale: CGFloat) -> UIImage? {
27+
let scaledSize = CGSize(width: size.width * scale, height: size.height * scale)
28+
return resize(targetSize: scaledSize)
29+
}
30+
}
31+
32+
extension CGSize {
33+
static let thumbnail:CGSize = CGSize(width: 50, height:50)
34+
}

Example/Demo/GalleryViewController.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import UIKit
22
import MHFacebookImageViewer
33

4-
class GalleryViewController:UIViewController {
4+
class WithImagesViewController:UIViewController {
55

66
var images:[UIImage] = Data.images
77

@@ -56,12 +56,14 @@ class GalleryViewController:UIViewController {
5656
}
5757
}
5858

59-
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
59+
override func viewWillTransition(
60+
to size: CGSize,
61+
with coordinator: UIViewControllerTransitionCoordinator) {
6062
updateLayout(size)
6163
}
6264
}
6365

64-
extension GalleryViewController:UICollectionViewDataSource {
66+
extension WithImagesViewController:UICollectionViewDataSource {
6567

6668
func numberOfSections(in collectionView: UICollectionView) -> Int {
6769
return 1
@@ -73,16 +75,20 @@ extension GalleryViewController:UICollectionViewDataSource {
7375
return images.count
7476
}
7577

76-
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
78+
func collectionView(
79+
_ collectionView: UICollectionView,
80+
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
81+
7782
let cell:ThumbCell = collectionView
7883
.dequeueReusableCell(withReuseIdentifier: ThumbCell.reuseIdentifier,
7984
for: indexPath) as! ThumbCell
8085
cell.imageView.image = images[indexPath.item]
8186

82-
// Setup Image Viewer
87+
// Setup Image Viewer with [UIImage]
8388
cell.imageView.setupImageViewer(
8489
images: images,
8590
initialIndex: indexPath.item)
91+
8692
return cell
8793
}
8894
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import UIKit
2+
import MHFacebookImageViewer
3+
import SDWebImage
4+
5+
class WithURLViewController:UIViewController {
6+
7+
lazy var imageView:UIImageView = {
8+
let iv = UIImageView()
9+
10+
// Set an image with low resolution.
11+
iv.image = Data.images[0].resize(targetSize: .thumbnail)
12+
13+
// Setup Image Viewer With URL
14+
iv.setupImageViewer(url: Data.imageUrls[0])
15+
return iv
16+
}()
17+
18+
override func loadView() {
19+
super.loadView()
20+
view = UIView()
21+
view.backgroundColor = .white
22+
view.addSubview(imageView)
23+
imageView.translatesAutoresizingMaskIntoConstraints = false
24+
imageView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor, constant: 20).isActive = true
25+
imageView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20).isActive = true
26+
imageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
27+
imageView.heightAnchor.constraint(equalToConstant: 100).isActive = true
28+
}
29+
30+
override func viewDidLoad() {
31+
super.viewDidLoad()
32+
33+
// for debugging purposes, just clear the download images
34+
SDImageCache.shared.clear(with: .all, completion: nil)
35+
}
36+
}

0 commit comments

Comments
 (0)