2
저는 IOS 개발의 초보자입니다. NSURLSession 및 didReciveData 메서드를 사용하여 이미지를 다운로드하는 방법을 알려줄 수 있습니까? 이미지 업로드 진행 상태와 함께 진행보기가 필요합니다. 나는 NSUrlSession을 만든 후에 붙어있다. 도와주세요.Swift IOS : NSURLSession 및 didReciveData를 사용하여 이미지를 다운로드하는 방법?
class ViewController: UIViewController, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate {
@IBOutlet weak var progressLabel: UILabel!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var progressView: UIProgressView!
@IBOutlet weak var downloadButton: UIButton!
@IBAction func downloadImage(sender: UIButton) {
let urlString = "https://img-fotki.yandex.ru/get/6111/8955119.3/0_7e1f6_a73b98a0_orig"
let url = NSURL(string: urlString)
var configuration: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
var session: NSURLSession = NSURLSession(configuration: self.configuration)
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
print("didReceiveData")
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
print("didReceiveRes")
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
let alert = UIAlertController(title: "Alert", message: error?.localizedDescription, preferredStyle: .Alert)
let alertAction = UIAlertAction(title: "Ok", style: .Default, handler: nil)
alert.addAction(alertAction)
presentViewController(alert, animated: true, completion: nil)
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
print("didReceiveSendData64")
var uploadProgress: Float = Float(totalBytesSent)/Float(totalBytesExpectedToSend)
progressView.progress = uploadProgress
}
}
웰컴, 당신이 대답에 만족은 투표를 잊어 대답에 동의하지 않는 경우. –
좋아요,하지만 평판이 15 세 이하이므로 투표 할 수 없습니다. –
알겠습니다. 행복한 코딩 감사합니다. –