title: Start UIScrollView with an image of particular index from another view
body: <p>I have a collectionView with multiple images. When tapping on one of them a new view is being opened - UIScrollView.</p>
  
  <p>Now every time UIScrollView starts with the first image of the array from collectionView.</p>
  
  <p>I am passing the index(imageNumber) of the image tapped to UIScrollView but i don't know how to make the first image shown to be the one with the index.</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
  
      var numberOfImagesPerALbum = data[albumNumber].count - 1
  
      for index in 0...numberOfImagesPerALbum {
  
          pageImages.append(UIImage(named: data[albumNumber][index]["image"]!)!)
  
  
      }
  
      var pageCount = pageImages.count
  
  
      // 2
      pageControl.currentPage = imageNumber
      println(imageNumber)
  
      pageControl.numberOfPages = pageCount
  
      // 3
      for _ in 0..&lt;pageCount {
          pageViews.append(nil)
      }
  
  
  }
  
  override func viewDidLayoutSubviews() {
      let pagesScrollViewSize = scrollView.frame.size
      scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * CGFloat(pageImages.count), pagesScrollViewSize.height)
  
      // 5
      loadVisiblePages()
  
  }
  
  
  func loadPage(page: Int) {
      if page &lt; 0 || page &gt;= pageImages.count {
          // If it's outside the range of what you have to display, then do nothing
          return
      }
  
      // 1
      if let pageView = pageViews[page] {
          // Do nothing. The view is already loaded.
  
      } else {
          // 2
          var frame = scrollView.bounds
          frame.origin.x = frame.size.width * CGFloat(page)
          frame.origin.y = 0.0
  
           3
          let newPageView = UIImageView(image: pageImages[page])
  
          newPageView.contentMode = .ScaleAspectFit
          newPageView.frame = frame
  
  
          scrollView.addSubview(newPageView)
  
          // 4
          pageViews[page] = newPageView
      }
  }
  
  func purgePage(page: Int) {
      if page &lt; 0 || page &gt;= pageImages.count {
          // If it's outside the range of what you have to display, then do nothing
          return
      }
  
      // Remove a page from the scroll view and reset the container array
      if let pageView = pageViews[page] {
          pageView.removeFromSuperview()
          pageViews[page] = nil
      }
  }
  
  func loadVisiblePages() {
      // First, determine which page is currently visible
      let pageWidth = scrollView.frame.size.width
      let page = Int(floor((scrollView.contentOffset.x * 2.0 + pageWidth) / (pageWidth * 2.0)))
      println("Page is \(page)")
  
      imageTitleLabel.text = data[albumNumber][page]["title"]
  
      imageDescriptionLabel.text = data[albumNumber][page]["text"]
  
      // Update the page control
      pageControl.currentPage = page
  
      // Work out which pages you want to load
      let firstPage = page - 1
      let lastPage = page + 1
  
      // Purge anything before the first page
      for var index = 0; index &lt; firstPage; ++index {
          purgePage(index)
      }
  
      // Load pages in our range
      for var index = firstPage; index &lt;= lastPage; ++index {
          loadPage(index)
      }
  
      // Purge anything after the last page
      for var index = lastPage+1; index &lt; pageImages.count; ++index {
          purgePage(index)
      }
  }
  
  func scrollViewDidScroll(scrollView: UIScrollView) {
      // Load the pages that are now on screen
      loadVisiblePages()
  }
  </code></pre>