IOS网络第一天 - 02SDWebImage
生活随笔
收集整理的這篇文章主要介紹了
IOS网络第一天 - 02SDWebImage
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
**************SDWebImage加載圖片
#import "HMAppsViewController.h" #import "HMApp.h" #import "UIImageView+WebCache.h"@interface HMAppsViewController () /*** 所有的應用數據*/ @property (nonatomic, strong) NSMutableArray *apps; @end@implementation HMAppsViewController#pragma mark - 懶加載 - (NSMutableArray *)apps {if (!_apps) {// 1.加載plistNSString *file = [[NSBundle mainBundle] pathForResource:@"apps" ofType:@"plist"];NSArray *dictArray = [NSArray arrayWithContentsOfFile:file];// 2.字典 --> 模型NSMutableArray *appArray = [NSMutableArray array];for (NSDictionary *dict in dictArray) {HMApp *app = [HMApp appWithDict:dict];[appArray addObject:app];}// 3.賦值self.apps = appArray; // _apps = appArray; }return _apps; }#pragma mark - 初始化方法 - (void)viewDidLoad {[super viewDidLoad];}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}#pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.apps.count; }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *ID = @"app";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];if (!cell) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];}// 取出模型HMApp *app = self.apps[indexPath.row];// 設置基本信息cell.textLabel.text = app.name;cell.detailTextLabel.text = app.download;// 下載圖片NSURL *url = [NSURL URLWithString:app.icon];UIImage *placeholder = [UIImage imageNamed:@"placeholder"]; // [cell.imageView sd_setImageWithURL:url placeholderImage:placeholder];// [cell.imageView sd_setImageWithURL:url placeholderImage:placeholder completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { // NSLog(@"----圖片加載完畢---%@", image); // }];//SDWebImageRetryFailed 嘗試重復下載//SDWebImageLowPriority 交互不下載SDWebImageOptions options = SDWebImageRetryFailed | SDWebImageLowPriority;[cell.imageView sd_setImageWithURL:url placeholderImage:placeholder options:options progress:^(NSInteger receivedSize, NSInteger expectedSize) { // 這個block可能會被調用多次NSLog(@"下載進度:%f", (double)receivedSize / expectedSize);} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {NSLog(@"----圖片加載完畢---%@", image);}];return cell; } @end***************HMAppDelegate.m
#import "SDWebImageManager.h"
/*** 當app接收到內存警告 */ - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {SDWebImageManager *mgr = [SDWebImageManager sharedManager];// 1.取消正在下載的操作 [mgr cancelAll];// 2.清除內存緩存 [mgr.imageCache clearMemory]; }?
轉載于:https://www.cnblogs.com/ios-g/p/4797615.html
總結
以上是生活随笔為你收集整理的IOS网络第一天 - 02SDWebImage的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实力认证 | 景联文科技获评“杭州市20
- 下一篇: 2021年线上录制视频汇总暨2022年样