Skip to content

Commit b4cc092

Browse files
committed
Update the iOS demo to use tableView showing the result
1 parent 0831167 commit b4cc092

5 files changed

Lines changed: 172 additions & 42 deletions

File tree

Example/SDWebImageLinkPlugin/Base.lproj/Main.storyboard

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="whP-gf-Uak">
3-
<device id="retina4_7" orientation="portrait">
4-
<adaptation id="fullscreen"/>
5-
</device>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="15505" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="Tq7-iw-Wfj">
3+
<device id="retina4_7" orientation="portrait" appearance="light"/>
64
<dependencies>
7-
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15509"/>
96
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
107
</dependencies>
118
<scenes>
@@ -22,10 +19,29 @@
2219
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
2320
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
2421
</view>
22+
<navigationItem key="navigationItem" id="w9S-dY-Nfz"/>
2523
</viewController>
2624
<placeholder placeholderIdentifier="IBFirstResponder" id="tc2-Qw-aMS" userLabel="First Responder" sceneMemberID="firstResponder"/>
2725
</objects>
28-
<point key="canvasLocation" x="305" y="433"/>
26+
<point key="canvasLocation" x="1244" y="432.23388305847078"/>
27+
</scene>
28+
<!--Navigation Controller-->
29+
<scene sceneID="3rD-Gb-ILC">
30+
<objects>
31+
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="Tq7-iw-Wfj" sceneMemberID="viewController">
32+
<toolbarItems/>
33+
<navigationBar key="navigationBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="We8-Mc-hcJ">
34+
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
35+
<autoresizingMask key="autoresizingMask"/>
36+
</navigationBar>
37+
<nil name="viewControllers"/>
38+
<connections>
39+
<segue destination="whP-gf-Uak" kind="relationship" relationship="rootViewController" id="8Xl-62-9zb"/>
40+
</connections>
41+
</navigationController>
42+
<placeholder placeholderIdentifier="IBFirstResponder" id="HXL-KM-XXP" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
43+
</objects>
44+
<point key="canvasLocation" x="304.80000000000001" y="432.23388305847078"/>
2945
</scene>
3046
</scenes>
3147
</document>

Example/SDWebImageLinkPlugin/SDAppDelegate.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
//
88

99
#import "SDAppDelegate.h"
10+
#import <SDWebImage/SDWebImage.h>
11+
#import <SDWebImageLinkPlugin/SDWebImageLinkPlugin.h>
1012

1113
@implementation SDAppDelegate
1214

1315
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
1416
{
1517
// Override point for customization after application launch.
18+
[SDImageLoadersManager.sharedManager addLoader:SDImageLinkLoader.sharedLoader];
19+
SDWebImageManager.defaultImageLoader = SDImageLoadersManager.sharedManager;
1620
return YES;
1721
}
1822

Lines changed: 132 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,150 @@
1-
//
2-
// SDViewController.m
3-
// SDWebImageLinkPlugin
4-
//
5-
// Created by lizhuoli1126@126.com on 11/21/2019.
6-
// Copyright (c) 2019 lizhuoli1126@126.com. All rights reserved.
7-
//
1+
/*
2+
* This file is part of the SDWebImage package.
3+
* (c) Olivier Poitrey <rs@dailymotion.com>
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
88

99
#import "SDViewController.h"
10-
#import <LinkPresentation/LinkPresentation.h>
1110
#import <SDWebImage/SDWebImage.h>
1211
#import <SDWebImageLinkPlugin/SDWebImageLinkPlugin.h>
1312

14-
@interface SDViewController ()
13+
@interface LinkTableViewCell : UITableViewCell
1514

1615
@property (nonatomic, strong) LPLinkView *linkView;
17-
@property (nonatomic, strong) UIImageView *imageView;
16+
17+
@end
18+
19+
@implementation LinkTableViewCell
20+
21+
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier url:(NSURL *)url {
22+
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
23+
_linkView = [[LPLinkView alloc] initWithURL:url];
24+
[self.contentView addSubview:_linkView];
25+
}
26+
return self;
27+
}
28+
29+
- (void)layoutSubviews {
30+
[super layoutSubviews];
31+
self.linkView.frame = CGRectInset(self.bounds, 20, 20);
32+
}
33+
34+
@end
35+
36+
@interface ImageTableViewCell : UITableViewCell
37+
38+
@property (nonatomic, strong) UILabel *customTextLabel;
39+
@property (nonatomic, strong) UIImageView *customImageView;
40+
41+
@end
42+
43+
@implementation ImageTableViewCell
44+
45+
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier url:(NSURL *)url {
46+
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
47+
_customImageView = [[UIImageView alloc] init];
48+
_customImageView.contentMode = UIViewContentModeScaleAspectFill;
49+
_customImageView.clipsToBounds = YES;
50+
_customImageView.layer.cornerRadius = 10;
51+
[self.contentView addSubview:_customImageView];
52+
_customTextLabel = [[UILabel alloc] init];
53+
[self.contentView addSubview:_customTextLabel];
54+
}
55+
return self;
56+
}
57+
58+
- (void)layoutSubviews {
59+
[super layoutSubviews];
60+
self.customImageView.frame = CGRectInset(self.bounds, 20, 20);
61+
[self.customTextLabel sizeToFit];
62+
self.customTextLabel.frame = CGRectMake(20, self.bounds.size.height - 20, self.bounds.size.width - 40, self.customTextLabel.frame.size.height);
63+
}
64+
65+
@end
66+
67+
@interface SDViewController () <UITableViewDelegate, UITableViewDataSource>
68+
69+
@property (nonatomic, assign) BOOL useLinkView;
70+
@property (nonatomic, strong) UITableView *tableView;
71+
@property (nonatomic, strong) NSArray<NSString *> *objects;
1872

1973
@end
2074

2175
@implementation SDViewController
2276

23-
- (void)viewDidLoad
24-
{
77+
- (void)viewDidLoad {
2578
[super viewDidLoad];
26-
[SDImageLoadersManager.sharedManager addLoader:SDImageLinkLoader.sharedLoader];
27-
SDWebImageManager.defaultImageLoader = SDImageLoadersManager.sharedManager;
79+
self.title = @"SDWebImage";
80+
self.navigationItem.rightBarButtonItem = [UIBarButtonItem.alloc initWithTitle:@"Clear Cache"
81+
style:UIBarButtonItemStylePlain
82+
target:self
83+
action:@selector(flushCache)];
84+
self.navigationItem.leftBarButtonItem = [UIBarButtonItem.alloc initWithTitle:@"Switch View"
85+
style:UIBarButtonItemStylePlain
86+
target:self
87+
action:@selector(switchView)];
88+
self.useLinkView = YES;
89+
self.objects = [NSArray arrayWithObjects:
90+
@"https://www.apple.com/",
91+
@"https://www.apple.com/music/",
92+
@"https://www.apple.com/apple-news/",
93+
@"https://www.icloud.com/",
94+
@"https://developer.apple.com/xcode/",
95+
@"https://developer.apple.com/swift/",
96+
@"https://developer.apple.com/testflight/",
97+
@"https://www.mozilla.org/en-US/firefox/",
98+
@"https://bing.com/",
99+
@"https://webkit.org/",
100+
nil];
101+
102+
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
103+
[self.view addSubview:self.tableView];
104+
self.tableView.delegate = self;
105+
self.tableView.dataSource = self;
106+
[self.tableView reloadData];
107+
}
108+
109+
- (void)switchView {
110+
self.useLinkView = !self.useLinkView;
111+
[self.tableView reloadData];
112+
}
113+
114+
- (void)flushCache {
115+
[SDWebImageManager.sharedManager.imageCache clearWithCacheType:SDImageCacheTypeAll completion:nil];
116+
}
117+
118+
#pragma mark - Table View
119+
120+
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
121+
return self.objects.count;
122+
}
123+
124+
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
125+
return 200;
126+
}
127+
128+
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
129+
static NSString *CellIdentifier = @"Cell";
130+
131+
NSURL *url = [NSURL URLWithString:self.objects[indexPath.row]];
132+
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
28133

29-
NSURL *url1 = [NSURL URLWithString:@"https://www.apple.com/iphone/"];
30-
NSURL *url2 = [NSURL URLWithString:@"https://webkit.org/"];
31-
self.linkView = [[LPLinkView alloc] initWithURL:url1];
32-
self.imageView = [[UIImageView alloc] init];
33-
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
34-
[self.view addSubview:self.linkView];
35-
[self.view addSubview:self.imageView];
134+
if (self.useLinkView) {
135+
if (![cell isKindOfClass:LinkTableViewCell.class]) {
136+
cell = [[LinkTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier url:url];
137+
}
138+
[((LinkTableViewCell *)cell).linkView sd_setImageWithURL:url];
139+
} else {
140+
if (![cell isKindOfClass:ImageTableViewCell.class]) {
141+
cell = [[ImageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier url:url];
142+
}
143+
((ImageTableViewCell *)cell).customTextLabel.text = url.host;
144+
[((ImageTableViewCell *)cell).customImageView sd_setImageWithURL:url];
145+
}
36146

37-
[self.linkView sd_setImageWithURL:url1 placeholderImage:nil options:SDWebImageFromLoaderOnly completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
38-
NSLog(@"%@", @"LPLinkView metadata load success");
39-
}];
40-
[self.imageView sd_setImageWithURL:url2 completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
41-
NSLog(@"%@", @"UIImageView image load success");
42-
}];
43-
}
44-
45-
- (void)viewDidLayoutSubviews {
46-
[super viewDidLayoutSubviews];
47-
self.linkView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 2);
48-
self.imageView.frame = CGRectMake(0, self.view.bounds.size.height / 2, self.view.bounds.size.width, self.view.bounds.size.height / 2);
147+
return cell;
49148
}
50149

51150
@end

Example/SDWebImageLinkPlugin/SDWebImageLinkPlugin-Info.plist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,10 @@
4545
<string>UIInterfaceOrientationLandscapeLeft</string>
4646
<string>UIInterfaceOrientationLandscapeRight</string>
4747
</array>
48+
<key>NSAppTransportSecurity</key>
49+
<dict>
50+
<key>NSAllowsArbitraryLoads</key>
51+
<true/>
52+
</dict>
4853
</dict>
4954
</plist>

SDWebImageLinkPlugin/Classes/LPLinkView+WebCache.m

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,27 @@ - (void)sd_setImageWithURL:(nullable NSURL *)url
5050
context:(nullable SDWebImageContext *)context
5151
progress:(nullable SDImageLoaderProgressBlock)progressBlock
5252
completed:(nullable SDExternalCompletionBlock)completedBlock {
53+
5354
__weak typeof(self) wself = self;
5455
[self sd_internalSetImageWithURL:url placeholderImage:placeholder options:options context:context setImageBlock:^(UIImage * _Nullable image, NSData * _Nullable imageData, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
5556
__strong typeof(self) sself = wself;
5657
LPLinkMetadata *metadata = imageURL.sd_linkMetadata;
5758
if (metadata) {
58-
sself.metadata = metadata;
59+
// Already exist
5960
} else if (image) {
6061
// Re-generate the metadata from local information
6162
metadata = [[LPLinkMetadata alloc] init];
6263
metadata.originalURL = url;
6364
metadata.URL = imageURL;
6465
metadata.imageProvider = [[NSItemProvider alloc] initWithObject:image];
6566
imageURL.sd_linkMetadata = metadata;
66-
sself.metadata = metadata;
67+
} else {
68+
metadata = [[LPLinkMetadata alloc] init];
69+
metadata.originalURL = url;
70+
metadata.URL = imageURL;
71+
imageURL.sd_linkMetadata = metadata;
6772
}
73+
sself.metadata = metadata;
6874
} progress:progressBlock completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
6975
if (completedBlock) {
7076
completedBlock(image, error, cacheType, imageURL);

0 commit comments

Comments
 (0)