如何将图像数组传递到另一个视图?

时间:2016-04-19 15:58:43

标签: ios arrays swift uiimageview

我有一个大约有12张图片的CollectionView。当我点击图像时,它应该在下一个屏幕上显示该图像被炸毁。问题是,在我点击特定图像后,它必须通过11个图像中的其余图像进行动画处理。我该怎么做呢?我有一些动画通过一系列图像动画的代码我不知道如何显示我点击的特定图像然后开始通过数组动画。

这是我的示例代码!

func convertUrlsToImage() {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
        for urls in self.arrayOfImageUrlsFromOtherView {
            if let url = NSURL(string: urls) {
                if let data = NSData(contentsOfURL: url) {
                    if let hingeImage = UIImage(data: data) {
                        self.convertedURLToImages.append(hingeImage)

                        dispatch_async(dispatch_get_main_queue(), { 
                            if self.galleryImageView.image == nil {
                                self.galleryImageView.image = self.convertedURLToImages[0]
                            }else{
                                print("Not empty")
                            }
                        })
                    }
                }
            }
        }
    })
}

func animateThroughArrayOfImages() {
    let currentIndex =  convertedURLToImages.indexOf(galleryImageView.image!)
    let nextIndex  = (currentIndex! + 1) % self.convertedURLToImages.count

    self.title = "\(nextIndex+1)"  + "/\(convertedURLToImages.count)"
    self.galleryImageView.image  = self.convertedURLToImages[nextIndex]
}

//附加代码

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    if let cell = collectionView.cellForItemAtIndexPath(indexPath) {

        performSegueWithIdentifier("nextView", sender: cell)

    }else{
        print("no good")
    }
}







override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {





    if let indexPath = self.collectionView?.indexPathForCell(sender as! ImageCollectionViewCell) {

        if segue.identifier == "nextView" {

            let galleryViewController = segue.destinationViewController as! GalleryViewController


           //We are passing the url to the next screen then making another request to show them
           let imageUrl = hingeImagesArray[indexPath.row]


            //passesd a single image url
            galleryViewController.imageUrlFromOtherScreen = imageUrl.imageUrl






            //Pasing array of urls 
            galleryViewController.arrayOfImageUrlsFromOtherView = hingeImageUrls

            //let myIndex = testImageArray[indexPath.row]








        }


    }

}

1 个答案:

答案 0 :(得分:0)

你的任务看起来很简单。只需为collectionView

实现此委托
optional func collectionView(_ collectionView: UICollectionView,
didSelectItemAtIndexPath indexPath: NSIndexPath)

然后从 indexPath 获取图像索引。然后,您可以将带有urls数组的索引传递到下一个屏幕,并使用它来设置初始图像。

但无论如何,将所有图像存储在一个内存阵列中并不是一个好主意。