日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IOS开发基础之异步下载网络图片第1部分

發布時間:2023/12/18 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS开发基础之异步下载网络图片第1部分 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

IOS開發基礎之異步下載網絡圖片第1部分

加入ATS

// LJAppInfo.h // 37-異步下載網絡圖片 // Created by 魯軍 on 2021/3/10. #import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface LJAppInfo : NSObject @property(nonatomic,copy)NSString *name; @property(nonatomic,copy)NSString *icon; @property(nonatomic,copy)NSString *download; +(instancetype)appInfoWithDict:(NSDictionary *)dict; //獲取所有的模型數據 +(NSArray *)appInfos; @end NS_ASSUME_NONNULL_END #import "LJAppInfo.h" @implementation LJAppInfo +(instancetype)appInfoWithDict:(NSDictionary *)dict{LJAppInfo *appInfo = [[self alloc] init];//kvc給屬性賦值[appInfo setValuesForKeysWithDictionary:dict];return appInfo;} //獲取所有的模型數據 +(NSArray *)appInfos{ //加載plist NSString *path =[[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil]; NSArray *array = [NSArray arrayWithContentsOfFile:path];//字典轉模型NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:10];//枚舉遍歷 使用Block遍歷[array enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {LJAppInfo *appInfo = [self appInfoWithDict:obj];[mArray addObject:appInfo]; }];return mArray.copy; //可變數組轉換成不可變數組 }; @end // LJAppInfoCell.h // 37-異步下載網絡圖片 // Created by 魯軍 on 2021/3/10. #import <UIKit/UIKit.h> @interface LJAppInfoCell : UITableViewCell @end // LJAppInfoCell.m // 37-異步下載網絡圖片 // Created by 魯軍 on 2021/3/10. #import "LJAppInfoCell.h" @implementation LJAppInfoCell - (void)awakeFromNib {[super awakeFromNib]; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated {[super setSelected:selected animated:animated]; } -(void)layoutSubviews{[super layoutSubviews];//NSLog(@"layoutSubviews"); } @end // // ViewController.m // 37-異步下載網絡圖片 // // Created by 魯軍 on 2021/3/9. //#import "ViewController.h" #import "LJAppInfo.h" #import "LJAppInfoCell.h" @interface ViewController ()<UITableViewDataSource> @property(nonatomic,strong) NSArray *appInfos; @property(nonatomic,strong)NSOperationQueue *queue; @end @implementation ViewController - (void)viewDidLoad {[super viewDidLoad];//測試NSLog(@"%@",self.appInfos); } //數據源方法 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{//1. 創建可重用的cellstatic NSString *reuseId = @"appInfo";LJAppInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];if(cell==nil){cell = [[LJAppInfoCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseId];}//2獲取數據 斤cell內部 控制賦值LJAppInfo *appInfo = self.appInfos[indexPath.row];//cell內部的子控件都是懶加載的//當返回cell之前 會調用 cell的layoutSubviews方法cell.textLabel.text = appInfo.name;cell.detailTextLabel.text = appInfo.download;//設置占位圖片cell.imageView.image = [UIImage imageNamed:@"user_default"];//同步下載圖片//模擬網絡較慢[self.queue addOperationWithBlock:^{[NSThread sleepForTimeInterval:0.5];//行高設置 80NSURL *url=[NSURL URLWithString:appInfo.icon];NSData *data = [NSData dataWithContentsOfURL:url];UIImage *img = [UIImage imageWithData:data];//主線程更新UI[[NSOperationQueue mainQueue] addOperationWithBlock:^{cell.imageView.image = img;//[cell setNeedsDisplay];[cell.imageView sizeToFit];}];}];//3返回cellreturn cell; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return self.appInfos.count; } //懶加載數據 -(NSOperationQueue *) queue{if(_queue==nil){_queue = [[NSOperationQueue alloc] init]; }return _queue; } //懶加載數據 -(NSArray *)appInfos{if(_appInfos==nil){_appInfos = [LJAppInfo appInfos];}return _appInfos; } @end

總結

以上是生活随笔為你收集整理的IOS开发基础之异步下载网络图片第1部分的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。