|
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 | + */ |
8 | 8 |
|
9 | 9 | #import "SDViewController.h" |
10 | | -#import <LinkPresentation/LinkPresentation.h> |
11 | 10 | #import <SDWebImage/SDWebImage.h> |
12 | 11 | #import <SDWebImageLinkPlugin/SDWebImageLinkPlugin.h> |
13 | 12 |
|
14 | | -@interface SDViewController () |
| 13 | +@interface LinkTableViewCell : UITableViewCell |
15 | 14 |
|
16 | 15 | @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; |
18 | 72 |
|
19 | 73 | @end |
20 | 74 |
|
21 | 75 | @implementation SDViewController |
22 | 76 |
|
23 | | -- (void)viewDidLoad |
24 | | -{ |
| 77 | +- (void)viewDidLoad { |
25 | 78 | [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]; |
28 | 133 |
|
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 | + } |
36 | 146 |
|
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; |
49 | 148 | } |
50 | 149 |
|
51 | 150 | @end |
0 commit comments