文件结构:
ImageCount 图片观察对象
ScoutOperation 图片操作队列
ImageParser 图片解析
SessionDelegate 图片下载代理
具体执行过程:
1、let scout = ImageScout()
2、 scout.scoutImage(atURL: imagePath) { (error, size, type) -> () in
设置返回时的回调
3、 operation.completionBlock = { [weak self] in
completion(operation.error, operation.size, operation.type)
self?.operations[urlString] = nil
}
请求数据时 拼接获得到的数据
4、extension ImageScout {
func didReceive(data: Data, task: URLSessionDataTask) {
guard let requestURL = task.currentRequest?.url?.absoluteString else { return }
guard let operation = operations[requestURL] else { return }
operation.append(data)
}
//完成时数据开始解析
func didComplete(with error: NSError?, task: URLSessionDataTask) {
guard let requestURL = task.currentRequest?.url?.absoluteString,
let operation = operations[requestURL]
else { return }
let completionError = error ?? ImageScout.error(withMessage: unableToParseErrorMessage, code: 101)
operation.terminate(with: completionError)
}
}