2017-07-20 147 views
1

我已經創建了一些代碼,以便從圖像選取器中提取圖像,並將圖像放入數組中。使用Swift 3或4的iOS重構代碼建議

我想將數組中的圖像放入另一個用於上傳到firebase的變量。我遇到的問題是,如果用戶沒有選擇8張圖片,那麼它會使程序崩潰,因爲新變量正在等待它們,所以我寫了一些If,Else代碼,但是它長時間地執行了大量的計數檢查陣列中的圖像。我期待看看這是否可以重構,如果有的話可能是switch語句。我相當新,所以也許我對交換機可以做什麼有錯誤的想法。我現在要讀更多關於它們的內容。

以下是我的代碼。

func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) { 
    if images.count > 8 { 

     imagePicker.dismiss(animated: true, completion: nil) 

     photoNumber() 

    }else { 
     if images.count == 1 { 
    photo1 = images[0] 
     }else { 
      print("Tony: No image in number 1") 
     } 
      if images.count == 2 { 
    photo1 = images[0] 
    photo2 = images[1] 
      }else { 
       print("Tony: No image in number 2") 
      } 
     if images.count == 3 { 
      photo1 = images[0] 
      photo2 = images[1] 
      photo3 = images[2] 
     }else { 
      print("Tony: No image in number 3") 
     } 
     if images.count == 4 { 
      photo1 = images[0] 
      photo2 = images[1] 
      photo3 = images[2] 
      image4 = images[3] 
     }else { 
      print("Tony: No image in number 4") 
     } 
     if images.count == 5 { 
      photo1 = images[0] 
      photo2 = images[1] 
      photo3 = images[2] 
      image4 = images[3] 
      image5 = images[4] 
     }else { 
      print("Tony: No image in number 5") 
     } 
     if images.count == 6 { 
      photo1 = images[0] 
      photo2 = images[1] 
      photo3 = images[2] 
      image4 = images[3] 
      image5 = images[4] 
      image6 = images[5] 
     }else { 
      print("Tony: No image in number 6") 
     } 
     if images.count == 7 { 
      photo1 = images[0] 
      photo2 = images[1] 
      photo3 = images[2] 
      image4 = images[3] 
      image5 = images[4] 
      image6 = images[5] 
      image7 = images[6] 
     }else { 
      print("Tony: No image in number 7") 
     } 
     if images.count == 8 { 
      photo1 = images[0] 
      photo2 = images[1] 
      photo3 = images[2] 
      image4 = images[3] 
      image5 = images[4] 
      image6 = images[5] 
      image7 = images[6] 
      image8 = images[7] 
     }else { 
      print("Tony: No image in number 8") 
     } 

    imagePicker.dismiss(animated: true, completion: nil) 
} 
} 
+0

你爲什麼要保持圖像的變量?你不能使用相同的數組&上解僱數組上傳圖像對象? – Ellen

+0

我已經嘗試過,但是當我交換變量時,我把圖片放入ie photo1中,圖片[0]上傳到firebase時調用它說圖片未定義,我推測這是因爲它是在調用圖片的函數中創建的選擇器 –

+0

但在運行時,您可以通過迭代遍歷數組而不是先檢查 – Ellen

回答

2

是,switch語句將在這裏工作,但是任何你與小的變化一遍又一遍地重複相同的代碼時,這是一個「代碼味道」,你應該想想做不同的方式它。

將值手動安裝到變量名爲photo1,photo2,photo3,image4,image5 .. image8是尷尬的。爲什麼你有一堆具有不同數字後綴的變量?這表明您應該將圖片保存到數組中,而不是從輸入的圖像數組手動映射到唯一變量。

func doneButtonDidPress(
    _ imagePicker: ImagePickerController, 
    images: [UIImage]) { 
    switch images.count { 
     case 0: 
     print("No images") //Nothing to see here 
     case 1: 
     photo1 = images[0] 
     case 2: 
     photo1 = images[0] 
     photo2 = images[1] 
     case 3: 
     photo1 = images[0] 
     photo2 = images[1] 
     photo3 = images[2] 
     case 4: 
     photo1 = images[0] 
     photo2 = images[1] 
     photo3 = images[2] 
     image4 = images[3] 
     case 5: 
     photo1 = images[0] 
     photo2 = images[1] 
     photo3 = images[2] 
     image4 = images[3] 
     image5 = images[4] 
     case 6: 
     photo1 = images[0] 
     photo2 = images[1] 
     photo3 = images[2] 
     image4 = images[3] 
     image5 = images[4] 
     image6 = images[5] 
     case 7: 
     photo1 = images[0] 
     photo2 = images[1] 
     photo3 = images[2] 
     image4 = images[3] 
     image5 = images[4] 
     image6 = images[5] 
     image7 = images[6] 
     case 8: 
     photo1 = images[0] 
     photo2 = images[1] 
     photo3 = images[2] 
     image4 = images[3] 
     image5 = images[4] 
     image6 = images[5] 
     image7 = images[6] 
     image8 = images[7] 
     default: 
     print("Too many images") 
    } 
} 
+0

謝謝@Duncan我會嘗試讓它們保存在一個數組中,當我創建一個名爲photos的數組時:[UIImage ]!然後嘗試爲其分配一張圖片,即照片[0] =圖片[0]圖像選擇器關閉時發生崩潰。 –

+0

感謝您的幫助我能夠使用您的建議和更改創建一個名爲photos的新UIImage陣列,然後將這些圖像傳遞到新陣列中,然後使用基於.count進行上傳的新陣列。這樣我不需要做,如果,否則或切換。 –

0

幫助後,我能夠創建一個名爲照片的新陣列UIImage。

var photos: Array<UIImage> = [] 

然後我就能夠通過圖像陣列舉行imagePicker的完成採摘函數創建的照片,並把它們放到新照片陣列,然後允許我使用基於photos.count到陣列將圖像上傳到firebase,然後將圖像傳遞給下一個視圖控制器以添加說明。

func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) { 

     if images.count > 8 { 
     imagePicker.dismiss(animated: true, completion: nil) 
      photoNumber() 
     }else { 
      photos = images // I added this instead of the If, Else statement or Switch statement 
     }