快速问答。我想从网址中提取下载的图像,并将其保存到UI图像。
我该怎么做呢?
fileprivate func beginDownload() {
let url = URL(string: "URL")!
let configuration = URLSessionConfiguration.default
let operationQueue = OperationQueue()
let urlSession = URLSession(configuration: configuration, delegate: self, delegateQueue: operationQueue)
let downloadTask = urlSession.downloadTask(with: url)
downloadTask.resume()
}以下是我的URL会话协议存根:
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
print(totalBytesWritten, totalBytesExpectedToWrite)
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
print("Finished dowloading file")
}发布于 2020-03-03 05:40:40
您需要从委托方法urlSession(_:downloadTask:didFinishDownloadingTo:)中创建的下载location加载图像
class Request: NSObject {
func getPicture() {
let url = URL(string: "https://media.tractorsupply.com/is/image/TractorSupplyCompany/1305371?$456$")!
let session = URLSession(configuration: .default,
delegate: self,
delegateQueue: nil)
session.downloadTask(with: url).resume()
}
}
extension Request: URLSessionDownloadDelegate {
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didFinishDownloadingTo location: URL) {
guard let data = try? Data(contentsOf: location),
let image = UIImage(data: data) else { return }
print(image)
}
}
Request().getPicture()https://stackoverflow.com/questions/60496764
复制相似问题