【问题标题】:How to pass image string value to the imageslide using swift如何使用 swift 将图像字符串值传递给图像幻灯片
【发布时间】:2017-05-11 09:45:29
【问题描述】:

我正在我的应用程序中使用 swift 2.2 开发一个应用程序,我通过引用此链接 https://github.com/zvonicek/ImageSlideshow 使用图像幻灯片放映我想传递带有连接 url 的图像字符串....

图像字符串是从 json 数据中获取的:

{

   "Values Of Image":[{

   "image":"apple.png,jewels.png,gun.png,mango.png"

   }]

}

这只是一个示例 json 数据,但我正在获取这样的数据

现在我可以分离字符串,现在我想将值传递给 alamofire 源(imageslide)

在控制器中编码:

在视图中确实加载了:

let MyImageSlide =  json["Values Of Image"][0]["image"].stringValue
        let parts = MyImageSlide.componentsSeparatedByString(",")
        for numbers in parts{
            print("image String:\(numbers)")
         let alamofireSource = [AlamofireSource(urlString: "https://www.something.com" + numbers as String)!]
         self.SecondImageShow.backgroundColor = UIColor.whiteColor()
         self.SecondImageShow.pageControlPosition = PageControlPosition.UnderScrollView
         self.SecondImageShow.pageControl.currentPageIndicatorTintColor = UIColor.lightGrayColor()
         self.SecondImageShow.pageControl.pageIndicatorTintColor = UIColor.blackColor()
         self.SecondImageShow.contentScaleMode = UIViewContentMode.ScaleAspectFill
         self.SecondImageShow.setImageInputs(alamofireSource)
        }

我的回复中的图片总数为四张……但它只获取最后一张图片…… 帮我获取所有图片

【问题讨论】:

    标签: ios json swift image swift2


    【解决方案1】:

    它只显示最后一个,因为您在循环中调用了setImageInputs。

    setImageInputs 应该调用图像源数组。您的更改应如下所示。

    // create array of image sources
    var images = [InputSource]()
    
    for numbers in parts{
         let alamofireSource = AlamofireSource(urlString: "https://www.something.com" + numbers as String)!
        images.append(alamofireSource)
    }
    
    self.SecondImageShow.backgroundColor = UIColor.whiteColor()
    self.SecondImageShow.pageControlPosition = PageControlPosition.UnderScrollView
    self.SecondImageShow.pageControl.currentPageIndicatorTintColor = UIColor.lightGrayColor()
    self.SecondImageShow.pageControl.pageIndicatorTintColor = UIColor.blackColor()
    self.SecondImageShow.contentScaleMode = UIViewContentMode.ScaleAspectFill
    
    // load the array here
    self.SecondImageShow.setImageInputs(images)
    

    参考这里的细节类https://github.com/zvonicek/ImageSlideshow/blob/master/ImageSlideshow/Classes/Core/ImageSlideshow.swift

    【讨论】:

    • 在 images.append(alamofiresource) 处出现错误,即参数 [AlamofireSource] 不符合预期的类型输入存储
    • 试试改成var images = [AlamofireSource]()
    • 给出与 images.append(alamofiresource) 相同的错误,即参数 [AlamofireSource] 不符合预期的类型 AlamofireSource
    • 我忘记从循环内的[Alamofire .. ] 中删除[ ]。检查更新的代码
    猜你喜欢
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多