Skip to content

Commit 8f654bf

Browse files
committed
使用原生界面浏览图片#24
1 parent 99b4b0d commit 8f654bf

File tree

5 files changed

+60
-4
lines changed

5 files changed

+60
-4
lines changed

Podfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ target 'ruby-china-ios' do
99
pod 'Kingfisher', '~> 2.4'
1010
pod 'SnapKit', '~> 0.22'
1111
pod 'FontAwesome.swift', :git => 'https://github.com/thii/FontAwesome.swift.git', :branch => 'swift-2.3'
12+
pod 'SKPhotoBrowser', '~> 3.1'
1213

1314
pod 'MBProgressHUD'
1415
pod 'MJRefresh'

Podfile.lock

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ PODS:
1111
- Result (2.1.3)
1212
- Router (1.0.0)
1313
- SideMenu (1.2.1)
14+
- SKPhotoBrowser (3.1.2)
1415
- SnapKit (0.22.0)
1516
- SwiftyJSON (2.4.0)
1617
- Turbolinks (1.0.2)
@@ -26,6 +27,7 @@ DEPENDENCIES:
2627
- MJRefresh
2728
- Router (~> 1.0)
2829
- SideMenu (~> 1.0)
30+
- SKPhotoBrowser (~> 3.1)
2931
- SnapKit (~> 0.22)
3032
- SwiftyJSON (~> 2.0)
3133
- Turbolinks (from `https://github.com/turbolinks/turbolinks-ios.git`)
@@ -57,12 +59,13 @@ SPEC CHECKSUMS:
5759
Result: 18e1a7597fbcca5923c565b40a25a98afafdee02
5860
Router: 6088d6d321e097070d5ea4bd792cb015dfa0fbb4
5961
SideMenu: d38e42ab18a5c0875a77cfec7e7cecea72a69518
62+
SKPhotoBrowser: e942c03c3206b5cf229fd4ce56b872bce800124a
6063
SnapKit: 0dd2fd157330f1ea11fd84da13e6be8a7a22bae0
6164
SwiftyJSON: 96918c1bf505efa50c4f72957018dd3452090c9c
6265
Turbolinks: d8209bab43803adae8c06d434368e16479023407
6366
UITableView+FDTemplateLayoutCell: 234e1582bcc4e18461af91155123bb96538ed030
6467
YYKeyboardManager: ff53d2e80d1808567a7f004028bd200d9469a89f
6568

66-
PODFILE CHECKSUM: 7199582b636d3565b7e5061cd29bd660bf79c27b
69+
PODFILE CHECKSUM: 8bd0bec9e089422171a89cd9bd29500de02f3d5b
6770

6871
COCOAPODS: 1.1.1

ruby-china-ios.xcodeproj/project.pbxproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
24B228EF1DAA8B1600DEC44D /* Topic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24B228EE1DAA8B1600DEC44D /* Topic.swift */; };
4444
24B228F31DAA972500DEC44D /* String+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24B228F21DAA972500DEC44D /* String+Ext.swift */; };
4545
24BA25451DBF858D004A801C /* TopicDetailsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24BA25441DBF858D004A801C /* TopicDetailsViewController.swift */; };
46+
24DE002B1DC1CA2C004AA741 /* CustomSKImageCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24DE002A1DC1CA2C004AA741 /* CustomSKImageCache.swift */; };
4647
24E5B4001D6B2C82006515E3 /* ThemeNavigationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24E5B3FF1D6B2C82006515E3 /* ThemeNavigationController.swift */; };
4748
24FDFBBC1DBBAE06004BB9A6 /* UIBarButtonItem+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24FDFBBB1DBBAE06004BB9A6 /* UIBarButtonItem+Ext.swift */; };
4849
961580A71D477A7600B32BDA /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 961580A61D477A7600B32BDA /* Assets.xcassets */; };
@@ -95,6 +96,7 @@
9596
24B228EE1DAA8B1600DEC44D /* Topic.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Topic.swift; sourceTree = "<group>"; };
9697
24B228F21DAA972500DEC44D /* String+Ext.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Ext.swift"; sourceTree = "<group>"; };
9798
24BA25441DBF858D004A801C /* TopicDetailsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TopicDetailsViewController.swift; sourceTree = "<group>"; };
99+
24DE002A1DC1CA2C004AA741 /* CustomSKImageCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomSKImageCache.swift; sourceTree = "<group>"; };
98100
24E5B3FF1D6B2C82006515E3 /* ThemeNavigationController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ThemeNavigationController.swift; sourceTree = "<group>"; };
99101
24FDFBBB1DBBAE06004BB9A6 /* UIBarButtonItem+Ext.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIBarButtonItem+Ext.swift"; sourceTree = "<group>"; };
100102
24FDFBBD1DBBB53D004BB9A6 /* Ruby China.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "Ruby China.entitlements"; sourceTree = "<group>"; };
@@ -158,6 +160,7 @@
158160
24A6A3ED1DB7B82500EC89DB /* LikesService.swift */,
159161
24742CE71D50FE2E00D16CA9 /* OAuth2.swift */,
160162
24742CE81D50FE2E00D16CA9 /* TurbolinksSessionLib.swift */,
163+
24DE002A1DC1CA2C004AA741 /* CustomSKImageCache.swift */,
161164
);
162165
path = Libraries;
163166
sourceTree = "<group>";
@@ -441,6 +444,7 @@
441444
24742CF21D50FE2E00D16CA9 /* NewTopicViewController.swift in Sources */,
442445
24742CFB1D50FE2E00D16CA9 /* DeviceService.swift in Sources */,
443446
24742CFF1D50FE2E00D16CA9 /* Error.swift in Sources */,
447+
24DE002B1DC1CA2C004AA741 /* CustomSKImageCache.swift in Sources */,
444448
);
445449
runOnlyForDeploymentPostprocessing = 0;
446450
};
@@ -565,8 +569,8 @@
565569
buildSettings = {
566570
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
567571
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
568-
DEVELOPMENT_TEAM = A38T799N53;
569572
CODE_SIGN_ENTITLEMENTS = "ruby-china-ios/Ruby China.entitlements";
573+
DEVELOPMENT_TEAM = A38T799N53;
570574
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
571575
INFOPLIST_FILE = "ruby-china-ios/Info.plist";
572576
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
@@ -584,8 +588,8 @@
584588
buildSettings = {
585589
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
586590
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
587-
DEVELOPMENT_TEAM = A38T799N53;
588591
CODE_SIGN_ENTITLEMENTS = "ruby-china-ios/Ruby China.entitlements";
592+
DEVELOPMENT_TEAM = A38T799N53;
589593
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
590594
INFOPLIST_FILE = "ruby-china-ios/Info.plist";
591595
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// CustomSKImageCache.swift
3+
// ruby-china-ios
4+
//
5+
// Created by kelei on 16/10/27.
6+
// Copyright © 2016年 ruby-china. All rights reserved.
7+
//
8+
9+
import SKPhotoBrowser
10+
import Kingfisher
11+
12+
class CustomImageCache: SKImageCacheable {
13+
private let cache = KingfisherManager.sharedManager.cache
14+
15+
func imageForKey(key: String) -> UIImage? {
16+
guard let cacheType = cache.isImageCachedForKey(key).cacheType else { return nil }
17+
18+
switch cacheType {
19+
case .Disk: return cache.retrieveImageInDiskCacheForKey(key)
20+
case .Memory: return cache.retrieveImageInMemoryCacheForKey(key)
21+
default: return nil
22+
}
23+
}
24+
25+
func setImage(image: UIImage, forKey key: String) {
26+
cache.storeImage(image, forKey: key)
27+
}
28+
29+
func removeImageForKey(key: String) {
30+
cache.removeImageForKey(key)
31+
}
32+
}

ruby-china-ios/Libraries/TurbolinksSessionLib.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Turbolinks
1010
import WebKit
1111
import Router
1212
import SafariServices
13+
import SKPhotoBrowser
1314

1415
class TurbolinksSessionLib: NSObject {
1516
static let sharedInstance: TurbolinksSessionLib = {
@@ -168,6 +169,18 @@ class TurbolinksSessionLib: NSObject {
168169
UIApplication.currentViewController()?.presentViewController(navController, animated: true, completion: nil)
169170
}
170171

172+
private func presentImageBrowserController(url: NSURL) {
173+
if (SKCache.sharedCache.imageCache as? CustomImageCache) == nil {
174+
SKCache.sharedCache.imageCache = CustomImageCache()
175+
}
176+
177+
let photo = SKPhoto.photoWithImageURL(url.absoluteString!)
178+
photo.shouldCachePhotoURLImage = true
179+
180+
let browser = SKPhotoBrowser(photos: [photo])
181+
UIApplication.currentViewController()?.presentViewController(browser, animated: true, completion: nil)
182+
}
183+
171184
private func pushNodeTopicsController(nodeID: Int) {
172185
let controller = TopicsViewController()
173186
controller.load(listType: .last_actived, nodeID: nodeID, offset: 0)
@@ -241,7 +254,10 @@ extension TurbolinksSessionLib: WKNavigationDelegate {
241254
}
242255

243256
if let url = navigationAction.request.URL {
244-
if let host = url.host where host != NSURL(string: ROOT_URL)!.host! {
257+
if let ext = url.pathExtension?.lowercaseString where (["jpg", "png", "gif"].filter{ ext.hasPrefix($0) }).count > 0 {
258+
// 查看图片
259+
presentImageBrowserController(url)
260+
} else if let host = url.host where host != NSURL(string: ROOT_URL)!.host! {
245261
// 外部网站, open in SafariView
246262
safariOpen(url)
247263
} else if let path = url.path {

0 commit comments

Comments
 (0)