我将如何在图像阵列中查看下一个图像



在此功能中,我需要更改图像视图的图像,将其移至数组中的下一个图像上。

private func updateImage(index: Int) {
    for x in 0..<urlArray.count
    {
        let imageUrl:URL = URL(string: "(urlArray.object(at: x) as! String)")!
        let request:URLRequest = URLRequest.init(url: imageUrl)
        NSURLConnection.sendAsynchronousRequest(request, queue: OperationQueue.main, completionHandler: { (response, imageDta, error) in
            if (error == nil)
            {
                self.imagesArray.add(UIImage(data: imageDta!)!)
                if self.imagesArray.count > 0
                {
                    self.iv.image = self.imagesArray.object(at: 0) as! UIImage
                }
            }else{
                print("ERROR - (error!)")
            }
        })
    }
}

但是,当前,所使用的图像被设置为我数组中的索引0的图像。如果此功能多次称为此功能,我将如何移至下一个图像?

这是我主要需要帮助的行:

self.iv.image = self.imagesArray.object(at: 0) as! UIImage

更新:我尝试将建议的代码charly pico添加到我的函数中,但似乎并没有转移到阵列中的下一个图像上,我认为这与它的调用方式有关。我将详细介绍我的文件的构造方式,并有望澄清我要如何使用更改图像代码。

var urlArray:NSMutableArray! = NSMutableArray()
var imagesArray:NSMutableArray! = NSMutableArray()

这些固定URL和图像的数组不在任何功能之外。

func GetTheStories() {
    let ref = FIRDatabase.database().reference().child("Content")
    ref.observe(FIRDataEventType.value, with: {(snapshot) in
        self.fileArray.removeAll()
        if snapshot.childrenCount>0{
            for content in snapshot.children.allObjects as![FIRDataSnapshot]{
                let contentInfo = content.value as? [String: String]
                let contentType = contentInfo?["contentType"]
                let storyLink = contentInfo?["pathToImage"]
                let content = storyLink   
                self.urlArray = NSMutableArray.init(array:[storyLink])                    
            }   
        }  
    })
}

这是我称之为将URL从我的firebase服务器附加到URLSARRAY的功能。

override func viewDidAppear(_ animated: Bool) {
    for x in 0..<urlArray.count
    {
        let imageUrl:URL = URL(string: "(urlArray.object(at: x) as! String)")!
        let request:URLRequest = URLRequest.init(url: imageUrl)
        NSURLConnection.sendAsynchronousRequest(request, queue: OperationQueue.main, completionHandler: { (response, imageDta, error) in
            if (error == nil)
            {
                self.imagesArray.add(UIImage(data: imageDta!)!)
                if self.imagesArray.count > 0
                {
                    self.iv.image = self.imagesArray.object(at: 0) as! UIImage
                }
            }else{
                print("ERROR - (error!)")
            }
        })
    }
}

这是创建和解释的功能,它允许我在屏幕加载

时在ImageView上设置初始图像
private func updateImage(index: Int) {
    var imageToPresent = 0
    for x in 0..<urlArray.count
    {
        let imageUrl:URL = URL(string: "(urlArray.object(at: x) as! String)")!
        let request:URLRequest = URLRequest.init(url: imageUrl)
        NSURLConnection.sendAsynchronousRequest(request, queue: OperationQueue.main, completionHandler: { (response, imageDta, error) in
            if (error == nil)
            {
                self.imagesArray.removeAllObjects()
                self.imagesArray.add(UIImage(data: imageDta!)!)
                if self.imagesArray.count > 0
                {
                    for x in 0..<self.imagesArray.count
                    { 
                        if self.iv.image == self.imagesArray.object(at: x) as! UIImage
                        {
                            imageToPresent = x + 1 
                            if imageToPresent >= self.imagesArray.count
                            {   
                                imageToPresent = 0
                            }
                        }
                    }
                    self.iv.image = self.imagesArray.object(at: imageToPresent) as! UIImage
                }
            }else{
                print("ERROR - (error!)")
            }
        })
    } 
}

这是将图像更新为数组中下一个图像的函数。但是,我认为这也无法正常工作,因为我尝试在下面应用Charly的代码是不正确的。此功能在ViewDidload中称为

updateImage(index: 0)

,它被要求在此处更新图像。当分段的进度条更改为下一个功能时,调用此功能。

func segmentedProgressBarChangedIndex(index: Int) {
    print("Now showing index: (index)")
    updateImage(index: index)
}

我知道为什么我不使用按钮和操作有点混乱,这主要是因为我试图创建一个Instagram风格的故事,并使用完成或屏幕敲击后的片段,更改阵列中的下一个图像。我知道有很多要看的东西,但我认为这将解释如何更详细地使用图像阵列。

所以,可以说您的imagesArray包含2个图像,我建议您进行以下操作:

//Create a @IBAction and attach it to a UIButton in your Storyboard as a touchUpInside action.
@IBAction func changeImage()
{
   //Set a variable with Int as 0.
   var imageToPresent = 0
   //Create a for to check image by image inside the array.
   for x in 0..<imagesArray.count
   {
      //Compare the image at self.iv with the image in imagesArray at index x. And if the images are the same add a + 1 to imageToPresent.
      if self.iv.image == imagesArray.object(at: x) as! UIImage
      {
         imageToPresent = x + 1
         //Check if imageToPresent isn't bigger or equal than imagesArray, otherwise we will get an error and the App will crash.
         if imageToPresent >= imagesArray.count
         {
            //If imageToPreset if bigger or equal to imagesArray.count, it means that there are no more images at a higher index, so we return imageToPresent to 0.
            imageToPresent = 0
         }
      }
   }
   //Set the new image of imagesArray at index imageToPresent as UIImage to self.iv.
   self.iv.image = imagesArray.object(at: imageToPresent) as! UIImage
}

每次您要更改self.iv image时,都需要触发触发操作的按钮。

更新

因此,我添加了一个NSTIMER来模拟您的Progress View在3秒内完成动画制作,在3秒钟后,它将图像旋转到另一个图像,您可以在for之后添加此代码,从而加载来自URL的所有图像:

Timer.scheduledTimer(withTimeInterval: 3.0, repeats: true) { (timer) in
        self.changeImage()
    }

首先将您的Urlarray更新到以下内容:

urlArray = NSMutableArray.init(array: ["https://firebasestorage.googleapis.com/v0/b/motive-73352.appspot.com/o/Content%2F20170525130622.jpg?alt=media&token=1e654c60-2f47-43c3-9298-b0282d27f66c","https://www.videomaker.com/sites/videomaker.com/files/styles/magazine_article_primary/public/articles/18984/363%20C03%20Lighting%20primary.png"])

因此,您至少有2张图像来旋转。

更改线以使用要传递到您的函数的索引

self.iv.image = self.imagesArray.object(at: index) as! UIImage

此功能正在尝试一次执行很多事情,因此很难告诉如何使它做您想要的事情,但是,您确实有一个索引参数。在这里而不是0:

  if self.imagesArray.count > index
  {
        self.iv.image = self.imagesArray.object(at: index) as! UIImage
  }

将等待该图像加载,然后将其设置为视图。

注意:您需要检查索引是否在范围内

最新更新