| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- /*
- * This file is part of the SDWebImage package.
- * (c) Olivier Poitrey <rs@dailymotion.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- #import "MasterViewController.h"
- #import "DetailViewController.h"
- #import <SDWebImage/SDWebImage.h>
- #import <SDWebImageWebPCoder/SDWebImageWebPCoder.h>
- @interface MyCustomTableViewCell : UITableViewCell
- @property (nonatomic, strong) UILabel *customTextLabel;
- @property (nonatomic, strong) SDAnimatedImageView *customImageView;
- @end
- @implementation MyCustomTableViewCell
- - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
- if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
- _customImageView = [[SDAnimatedImageView alloc] initWithFrame:CGRectMake(20.0, 2.0, 60.0, 40.0)];
- [self.contentView addSubview:_customImageView];
- _customTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 12.0, 200, 20.0)];
- [self.contentView addSubview:_customTextLabel];
-
- _customImageView.clipsToBounds = YES;
- _customImageView.contentMode = UIViewContentModeScaleAspectFill;
- }
- return self;
- }
- @end
- @interface MasterViewController ()
- @property (nonatomic, strong) NSMutableArray<NSString *> *objects;
- @end
- @implementation MasterViewController
- - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- self.title = @"SDWebImage";
- self.navigationItem.rightBarButtonItem = [UIBarButtonItem.alloc initWithTitle:@"Clear Cache"
- style:UIBarButtonItemStylePlain
- target:self
- action:@selector(flushCache)];
-
- [[SDImageCodersManager sharedManager] addCoder:[SDImageWebPCoder sharedCoder]]; // For WebP static/animated image
- [[SDImageCodersManager sharedManager] addCoder:[SDImageHEICCoder sharedCoder]]; // For HEIC static/animated image. Animated image is new introduced in iOS 13, but it contains performance issue for now.
-
- // HTTP NTLM auth example
- // Add your NTLM image url to the array below and replace the credentials
- [SDWebImageDownloader sharedDownloader].config.username = @"httpwatch";
- [SDWebImageDownloader sharedDownloader].config.password = @"httpwatch01";
- [[SDWebImageDownloader sharedDownloader] setValue:@"SDWebImage Demo" forHTTPHeaderField:@"AppName"];
- [SDWebImageDownloader sharedDownloader].config.executionOrder = SDWebImageDownloaderLIFOExecutionOrder;
-
- self.objects = [NSMutableArray arrayWithObjects:
- @"http://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?0.35786508303135633", // requires HTTP auth, used to demo the NTLM auth
- @"http://assets.sbnation.com/assets/2512203/dogflops.gif",
- @"https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif",
- @"http://apng.onevcat.com/assets/elephant.png",
- @"http://www.ioncannon.net/wp-content/uploads/2011/06/test2.webp",
- @"http://www.ioncannon.net/wp-content/uploads/2011/06/test9.webp",
- @"http://littlesvr.ca/apng/images/SteamEngine.webp",
- @"http://littlesvr.ca/apng/images/world-cup-2014-42.webp",
- @"https://isparta.github.io/compare-webp/image/gif_webp/webp/2.webp",
- @"https://nokiatech.github.io/heif/content/images/ski_jump_1440x960.heic",
- @"https://nokiatech.github.io/heif/content/image_sequences/starfield_animation.heic",
- @"https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png",
- @"http://via.placeholder.com/200x200.jpg",
- nil];
- for (int i=1; i<25; i++) {
- // From http://r0k.us/graphics/kodak/, 768x512 resolution, 24 bit depth PNG
- [self.objects addObject:[NSString stringWithFormat:@"http://r0k.us/graphics/kodak/kodak/kodim%02d.png", i]];
- }
- }
- return self;
- }
- - (void)flushCache {
- [SDWebImageManager.sharedManager.imageCache clearWithCacheType:SDImageCacheTypeAll completion:nil];
- }
- #pragma mark - Table View
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return self.objects.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *CellIdentifier = @"Cell";
-
- static UIImage *placeholderImage = nil;
- if (!placeholderImage) {
- placeholderImage = [UIImage imageNamed:@"placeholder"];
- }
-
- MyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[MyCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
- cell.customImageView.sd_imageTransition = SDWebImageTransition.fadeTransition;
- cell.customImageView.sd_imageIndicator = SDWebImageActivityIndicator.grayIndicator;
- }
-
- cell.customTextLabel.text = [NSString stringWithFormat:@"Image #%ld", (long)indexPath.row];
- [cell.customImageView sd_setImageWithURL:[NSURL URLWithString:self.objects[indexPath.row]]
- placeholderImage:placeholderImage
- options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- NSString *largeImageURLString = [self.objects[indexPath.row] stringByReplacingOccurrencesOfString:@"small" withString:@"source"];
- NSURL *largeImageURL = [NSURL URLWithString:largeImageURLString];
- DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
- detailViewController.imageURL = largeImageURL;
- [self.navigationController pushViewController:detailViewController animated:YES];
- }
- @end
|