2017-04-14 6 views
0

ウェブサイトのURLから画像をダウンロードしている間に、アクティビティインジケータを画面に表示したい場合は、images.imageに表示します下の私のコード。イメージをダウンロードするたびにすぐにステータスが表示され、アクティビティインジケータは表示されません。ウェブを介して検索しているが、私はまだ理解していない。あなたはactivityIndicatorイメージが画面に表示されるまで、画像/アクティビティインジケータを作成します。

  • 任意のビューには追加されませんでした

    1. あなたはcompletionHandlerブロック

    レッツにactivityIndicatorを構成し、以下の理由

    let mygroup = DispatchGroup() 
    var activityIndicator = UIActivityIndicatorView() 
    
    func downloadpic(sender:UIButton){ 
        let catPictureURL = URL(string: addr)! 
    
        // Creating a session object with the default configuration. 
        // You can read more about it here https://developer.apple.com/reference/foundation/urlsessionconfiguration 
        let session = URLSession(configuration: .default) 
    
        // Define a download task. The download task will download the contents of the URL as a Data object and then you can do what you wish with that data. 
    
        let downloadPicTask = session.dataTask(with: catPictureURL) { (data, response, error) in 
         // The download has finished. 
         if let e = error { 
          print("Error downloading cat picture: \(e)") 
         } else { 
          // No errors found. 
          // It would be weird if we didn't have a response, so check for that too. 
          if let res = response as? HTTPURLResponse { 
           // put loading screen here 
           self.activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .white) 
           self.activityIndicator.frame = CGRect(x: 0, y: 0, width: 46, height: 46) 
           self.activityIndicator.startAnimating() 
           self.mygroup.enter() 
           print("downloading image") 
           print("Downloaded cat picture with response code \(res.statusCode)") 
           if let imageData = data { 
            // Finally convert that Data into an image and do what you wish with it. 
            self.images.image = UIImage(data: imageData) 
             self.mygroup.leave() 
             print("image already downloaded") 
             self.activityIndicator.stopAnimating() 
             self.activityIndicator.hidesWhenStopped = true 
    
    
            // Do something with your image. 
           } else { 
            print("Couldn't get image: Image is nil") 
           } 
          } else { 
           print("Couldn't get response code for some reason") 
          } 
         } 
        } 
    
        downloadPicTask.resume() 
    } 
    

    答えて

    1

    を助けてくださいあなたは、この問題を得ましたactivityIndicatorviewDidLoad()メソッドで設定します。

    self.activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray) 
    self.activityIndicator.frame = CGRect(x: 0, y: 0, width: 46, height: 46) 
    self.activityIndicator.hidesWhenStopped = true 
    
    view.addSubview(self.activityIndicator) 
    

    、その後dataTask

    self.activityIndicator.startAnimating() 
    

    を再開する前に、それをアニメーション化するために開始し、completionHandler

    self.activityIndicator.stopAnimating() 
    

    で最終的なコード、それを停止します。

    let mygroup = DispatchGroup() 
    var activityIndicator = UIActivityIndicatorView() 
    
    override func viewDidLoad() { 
        super.viewDidLoad() 
    
        self.activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray) 
        self.activityIndicator.frame = CGRect(x: 0, y: 0, width: 46, height: 46) 
        self.activityIndicator.hidesWhenStopped = true 
    
        view.addSubview(self.activityIndicator) 
    } 
    
    func downloadpic(sender: UIButton) { 
        let catPictureURL = URL(string: addr)! 
    
        // Creating a session object with the default configuration. 
        // You can read more about it here https://developer.apple.com/reference/foundation/urlsessionconfiguration 
        let session = URLSession(configuration: .default) 
    
        // Define a download task. The download task will download the contents of the URL as a Data object and then you can do what you wish with that data. 
    
        self.activityIndicator.startAnimating() 
    
        let downloadPicTask = session.dataTask(with: catPictureURL) { (data, response, error) in 
         // The download has finished. 
         if let e = error { 
          print("Error downloading cat picture: \(e)") 
         } else { 
          // No errors found. 
          // It would be weird if we didn't have a response, so check for that too. 
          if let res = response as? HTTPURLResponse { 
           // put loading screen here 
    
           self.mygroup.enter() 
           print("downloading image") 
           print("Downloaded cat picture with response code \(res.statusCode)") 
           if let imageData = data { 
            // Finally convert that Data into an image and do what you wish with it. 
            self.images.image = UIImage(data: imageData) 
            self.mygroup.leave() 
            print("image already downloaded") 
            self.activityIndicator.stopAnimating() 
    
            // Do something with your image. 
           } else { 
            print("Couldn't get image: Image is nil") 
           } 
          } else { 
           print("Couldn't get response code for some reason") 
          } 
         } 
        } 
    
        downloadPicTask.resume() 
    } 
    

    結果を!

    enter image description here

    +0

    問題は活動の指標が停止し、その後すぐに始めるということですので、私はdidntの負荷は、上記のことを含まなかった私はすでにビューの活動の指標を持っています。私がしようとしているのは、画像がまだダウンロードされている間に活動インジケータを表示することです。私はアプリが画像をダウンロードしてから画像がimages.imageに表示された後にアクティビティインジケータが起動するようにする必要があります –

    +0

    添付ファイルを見てください!私はそれがあなたが望むものだと思う。 –

    +0

    あなたは正しいです、私の間違い申し訳ありません。答えに感謝します。 –

    関連する問題