2017-10-04 5 views
0

저는 icarousel을 사용하고 있습니다. Firebase에서 가져온 이미지를 배열에 배치하고 순서대로 이미지를 표시하고 싶습니다. 그래서 함수를 만들었습니다. 사용자가 게시 할 수있는 최대 값은 한 번에 3 장의 사진이며 3 장의 사진을 배열에 배치합니다.firebase 이미지를 배열에 넣으십시오.

func bookImageArray() { 
     databaseRef.child("books").child(self.postID!).observeSingleEvent(of: .value, with: { (snapshot) in 


       //Check to see if a first image for the book exists 
      if snapshot.hasChild("image"){ 
       if let stringImage = self.imageNames { 


        let imageRef = self.storage.reference(forURL: "gs://gsignme-14416.appspot.com/images/\(stringImage)") 

        imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in 
         if error == nil { 


          self.ImageOne = UIImage(data: data!)! 
          imageArray.append(self.ImageOne) 


         imageArray = [self.ImageOne] 


         }else { 
          print("Error downloading image:") 
         } 

         self.carouselView.reloadData() 
         self.carouselView.type = .linear 

        })} 
       print("image exists") 
      } 

      //Check to see if a second image for the book exists 
      if snapshot.hasChild("imageTwo") { 
       if let stringImages = self.imagesTwo { 

        let imageRefs = self.storage.reference(forURL: "gs://gsignme-14416.appspot.com/images/\(stringImages)") 

        imageRefs.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in 
         if error == nil { 

          self.ImageTwo = UIImage(data: data!)! 
          imageArray.append(self.ImageTwo) 


          imageArray = [self.ImageOne,self.ImageTwo] 


         } else { 
          print("Error downloading image:") 
         } 
         self.carouselView.reloadData() 
         self.carouselView.type = .linear 

        })} 

       print("imageTwo exists") 

       //Check to see if a third image for the book exists 
      } 

      if snapshot.hasChild("imageThree") { 
       if let stringImage3 = self.imagesThree { 

        let imageRef = self.storage.reference(forURL: "gs://gsignme-14416.appspot.com/images/\(stringImage3)") 

        imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in 
         if error == nil { 

          self.ImageThree = UIImage(data: data!)! 
          imageArray.append(self.ImageThree) 


          imageArray = [self.ImageOne,self.ImageTwo,self.ImageThree] 

         }else { 
          print("Error downloading image:") 
         } 

         self.carouselView.reloadData() 
         self.carouselView.type = .linear 

        })} 
       print("imageThree exists") 

       } 
      } 

이미지가 배열에 배치됩니다. 그러나 때로는 세 장의 사진이 표시되지 않고 한 장의 사진 만 표시되는 경우도 있습니다. 그런 다음 3 장의 사진을 표시하면 사진이 순서대로 표시됩니다. 3 개의 그림이 있다고 생각되는 셀을 클릭 할 때 왜 3 개의 그림이 나타나고 때로는 2 개의 그림이 나타나거나 때로는 하나의 그림이 나타나는 지 확실하지 않습니다.

답변

0

이미 요소를 추가하고 있으므로 아래 작업을 수행 할 필요가 없습니다.

imageArray = [self.ImageOne] 

아래 확인하시기 바랍니다 :없이

var imageArray:[UIImage] = [] 

func bookImageArray() { 
    databaseRef.child("books").child(self.postID!).observeSingleEvent(of: .value, with: { (snapshot) in 
     //Check to see if a first image for the book exists 
     if snapshot.hasChild("image"){ 
      if let stringImage = self.imageNames { 
       let imageRef = self.storage.reference(forURL: "gs://gsignme-14416.appspot.com/images/\(stringImage)") 

       imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in 
        if error == nil { 
         if let imageOne = UIImage(data: data!) { 
          imageArray.insert(imageOne, at: imageArray.count) 

         } 
        }else { 
         print("Error downloading image:") 
        } 
        self.carouselView.reloadData() 
        self.carouselView.type = .linear 
       })} 
      print("image exists") 
     } 

     //Check to see if a second image for the book exists 
     if snapshot.hasChild("imageTwo") { 
      if let stringImages = self.imagesTwo { 
       let imageRefs = self.storage.reference(forURL: "gs://gsignme-14416.appspot.com/images/\(stringImages)") 
       imageRefs.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in 
        if error == nil { 
         if let imageTwo = UIImage(data: data!) { 
          imageArray.insert(imageTwo, at: imageArray.count) 
         } 
        } else { 
         print("Error downloading image:") 
        } 
        self.carouselView.reloadData() 
        self.carouselView.type = .linear 

       })} 

      print("imageTwo exists") 

      //Check to see if a third image for the book exists 
     } 

     if snapshot.hasChild("imageThree") { 
      if let stringImage3 = self.imagesThree { 
       let imageRef = self.storage.reference(forURL: "gs://gsignme-14416.appspot.com/images/\(stringImage3)") 
       imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in 
        if error == nil { 
         if let imageThree = UIImage(data: data!) { 
          imageArray.insert(imageThree, at: imageArray.count) 
         } 
        }else { 
         print("Error downloading image:") 
        } 
        self.carouselView.reloadData() 
        self.carouselView.type = .linear 
       })} 
      print("imageThree exists") 
     } 
    } 
}) 
print(imageArray) 
+0

'imageArray = [self.ImageOne]'이미지 순서가 표시 순서 – juelizabeth

+0

에 표시되지 않습니다 이미지가, 내가 좋아하는 것 그것을이 될 수 있도록 표시되는 imageOne, imageTwo, imageThree – juelizabeth

+0

내 업데이트 된 답변을 확인하십시오 –