Arrays 如何查看图像阵列中的下一个图像?
在这个函数中,我需要更改图像视图的图像,将其移动到数组中的下一个图像上Arrays 如何查看图像阵列中的下一个图像?,arrays,swift,xcode,indexing,Arrays,Swift,Xcode,Indexing,在这个函数中,我需要更改图像视图的图像,将其移动到数组中的下一个图像上 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:
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!)")
}
})
}
}
更新:我尝试将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])
}
}
})
}
这是我调用的函数,用于将来自Firebase服务器的URL附加到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!)")
}
})
}
}
这里调用它来更新图像。当分段进度条更改为下一个进度条时,将调用此函数
func segmentedProgressBarChangedIndex(index: Int) {
print("Now showing index: \(index)")
updateImage(index: index)
}
我知道为什么我不使用按钮和动作有点让人困惑,这主要是因为我试图创建一个Instagram风格的故事,其中的片段在完成或点击屏幕时,会将图像更改为阵列中的下一个。我知道有很多东西要看,但我想这将解释我想如何更详细地使用图像数组 更改行以使用传递到函数中的索引,如下所示
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
}
将等待加载该图像,然后将其设置为视图
注意:您需要检查索引是否在范围内,因此,假设您的
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
}
首先,将您的URLARY更新为:
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张图像可以进行图像旋转。明天我将对此进行拍摄,谢谢您的帮助!我曾尝试将此代码添加到updateImage函数中,但它似乎不太起作用。我将发布一个更新,更详细地展示代码是如何工作的。有什么建议吗?很抱歉,暂时没有回答,我很忙。为什么要使用分段进度条?没问题,我正在尝试使用从数据库中提取的内容创建自己的Instagram故事版本。这主要是为了一个原型来演示我最终想要实现的功能
if self.imagesArray.count > index
{
self.iv.image = self.imagesArray.object(at: index) as! UIImage
}
//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
}
Timer.scheduledTimer(withTimeInterval: 3.0, repeats: true) { (timer) in
self.changeImage()
}
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"])