gpt4 book ai didi

arrays - 如何查看图像数组中的下一张图像?

转载 作者:可可西里 更新时间:2023-11-01 02:04:23 25 4
gpt4 key购买 nike

在此函数中,我需要更改 ImageView 的图像,将其移动到数组中的下一个图像上。

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])
}
}
})
}

这是我调用的函数,用于将来 self 的 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!)")
}
})
}
}

这是 Charly 创建和解释的函数,它允许我在屏幕加载时在 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)

并在此处调用更新图像。当 Segmented 进度条变为下一个时调用此函数。

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 图像 时,您都需要触摸按钮来触发操作。

更新

所以我添加了一个 NSTimer 来模拟在 3 秒内你的 Progress View 完成动画,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 张图像来进行图像旋转。

关于arrays - 如何查看图像数组中的下一张图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44336160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com