swift 网络----利用URLSession的Block模式下载多张图片
来源:互联网 发布:点对点网速测试软件 编辑:程序博客网 时间:2024/06/07 17:34
先上效果图
再上源码
1. 对于URLSession做了个简单的封装,用来请求网络数据
import Foundationclass LJDownLoadNetImage: NSObject { static func request(_ method: String, url: String, callback: @escaping (_ data: Data?, _ response: URLResponse?, _ error: Error?) -> Void) { let session = URLSession.shared let request = NSMutableURLRequest(url: NSURL(string: url)! as URL) request.httpMethod = method let task = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in callback(data, response, error) }) task.resume() }}
import UIKitclass TFNetImageViewController: TFBaseViewController, UITableViewDataSource, UITableViewDelegate{ var TFTableView:UITableView! var titleItemArray = ["姓名","账号","爱好","职业","年薪"] var imageUrlArray = ["http://pica.nipic.com/2007-12-12/20071212235955316_2.jpg", "http://hangge.com/blog/images/logo.png", "https://www.iphonetrain.com/core/res/images/logo.png", "http://pica.nipic.com/2007-12-12/20071212235955316_2.jpg", "http://pica.nipic.com/2007-12-12/20071212235955316_2.jpg"]; override func viewDidLoad() { super.viewDidLoad() self.setTopNavBarTitle("个人信息") self.setTopNavBackButton() self.view.backgroundColor = UIColor.lightGray self.creatTable() } func creatTable(){ TFTableView = UITableView(frame: CGRect(x: 0, y: 64, width: AppWidth, height: AppHeight - 64),style:UITableViewStyle.grouped); TFTableView.delegate = self; TFTableView.dataSource = self; self.view.addSubview(TFTableView); TFTableView.register(TFNetImageTableViewCell.self, forCellReuseIdentifier: "identtifier") } //MARK: UITableViewDataSource func numberOfSections(in tableView: UITableView) -> Int { return 1; } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5; } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{ return 55; } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 10; } func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { return 1; } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cellIdentifier : String = "identtifier" let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! TFNetImageTableViewCell let urlStr = "https://raw.githubusercontent.com/onevcat/Kingfisher/master/images/kingfisher-\(indexPath.row + 1).jpg" //cell.setCellData((titleItemArray[indexPath.row] as NSString) as String , imageUrlStr: imageUrlArray[indexPath.row]) cell.setCellData(titleItemArray[indexPath.row], imageUrlStr: urlStr) return cell } //MARK: UITableViewDelegate func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true); } }
cell的代码
import UIKitclass TFNetImageTableViewCell: UITableViewCell { var titleImage : UIImageView! var titleLabel : UILabel! override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) self.creatCell() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) self.selectionStyle = UITableViewCellSelectionStyle.none // Configure the view for the selected state } func creatCell(){ //http://pica.nipic.com/2007-12-12/20071212235955316_2.jpg //设置label titleLabel = UILabel(frame: CGRect(x: 60, y: 18, width: 40, height: 14)) titleLabel.textAlignment = NSTextAlignment.center titleLabel.font = UIFont.systemFont(ofSize: 14.0) self.contentView.addSubview(titleLabel) /* 1. 直接加载本地的图片 */ /* titleImage = UIImageView(frame: CGRect(x: 0, y: 5, width: 40, height: 40)) titleImage.image = UIImage(named: "unicorn.png") titleImage.backgroundColor = UIColor.red; self.contentView.addSubview(titleImage) */ /* 2. 加载网络图片 */ /* //定义NSURL对象 let url = NSURL.init(string: "http://hangge.com/blog/images/logo.png") //从网络获取数据流 let data = NSData(contentsOf: url! as URL) //此处如果data有值的话,才去初始化image if (data != nil) { //通过数据流初始化图片 let newImage = UIImage(data: data! as Data) let titleImage = UIImageView(frame: CGRect(x: 0, y: 5, width: 40, height: 40)) titleImage.image = newImage self.contentView.addSubview(titleImage) } else{ /* 直接加载本地的图片 */ titleImage = UIImageView(frame: CGRect(x: 0, y: 5, width: 40, height: 40)) titleImage.image = UIImage(named: "unicorn.png") titleImage.backgroundColor = UIColor.red; self.contentView.addSubview(titleImage) } */ } func setCellData(_ labelNameStr:String, imageUrlStr:String) { titleLabel.text = labelNameStr as String /* 3. 利用封装的URLSession加载网络图片 */ LJDownLoadNetImage.request("GET", url: imageUrlStr) { (data, response, error) in //此处如果data有值的话,才去初始化image if (data != nil) { let newImage = UIImage(data: data! as Data) let titleImage = UIImageView(frame: CGRect(x: 0, y: 5, width: 40, height: 40)) titleImage.image = newImage self.contentView.addSubview(titleImage) } } /* //定义NSURL对象 let url = NSURL.init(string: imageUrlStr) //从网络获取数据流 let data = NSData(contentsOf: url! as URL) //此处如果data有值的话,才去初始化image if (data != nil) { //通过数据流初始化图片 let newImage = UIImage(data: data! as Data) let titleImage = UIImageView(frame: CGRect(x: 0, y: 5, width: 40, height: 40)) titleImage.image = newImage self.contentView.addSubview(titleImage) } else{ /* 直接加载本地的图片 */ titleImage = UIImageView(frame: CGRect(x: 0, y: 5, width: 40, height: 40)) titleImage.image = UIImage(named: "unicorn.png") titleImage.backgroundColor = UIColor.red; self.contentView.addSubview(titleImage) } */ }}
阅读全文
0 0
- swift 网络----利用URLSession的Block模式下载多张图片
- swift 网络----利用URLSession的Download Task模式下载多张图片
- swift 网络----利用URLSession的Data Task下载单张图片(包含图片的增量下载)
- SWift 利用post上传多张图片
- URLSession数据协议下载图片
- swift 利用Opration和OprationQueue来下载网络图片
- 下载多张图片
- URLSession 后台多任务下载
- 利用缓存存储网络下载的图片
- swift 利用原生请求上传多张图片和多个参数到服务器
- swift 使用Kingfisher进行网络图片的下载
- 利用AsyncTask下载网络图片
- 【Swift】上传图片(多张)
- Swift 3.0封装 URLSession 的GET/SET方法代替 Alamofire
- UIScrollView利用分页展示多张图片
- IOS 利用ASIHttpRequest 上传多张图片
- 利用opencv读取多张图片
- HttpURLConnection方式网上下载多张图片
- WebHubBot爬虫分享(每天可抓取500万以上)
- 问题:Objective-C 中 C 类型函数不识别 foundation 中的类
- 设计模式
- SpringSecurity学习笔记(三)——自定义登录界面读取数据库用户以及权限
- Active Job
- swift 网络----利用URLSession的Block模式下载多张图片
- 11.ar, nm, objdump
- 华北水利水电大学社会实践--国情社情调研团走进社区
- 慕课HTML基础知识复习
- fabric 开发环境
- 葵花宝典 六
- jsp session
- jQuery
- HDU1073 Online Judge【输入输出+字符串】