iOS:将图像幻灯片的setImageInputs设置为图像数组

时间:2017-04-16 17:54:13

标签: ios swift xcode image

我正在使用来自commit a93dcb0的图片幻灯片:

    iconArr = [UIImage(named: "home-min")!,UIImage(named: "category-
   min")!,UIImage(named: "settings-min")!,UIImage(named: "contact us-min")!,UIImage(named: "about us-min")!,UIImage(named: "logout")!]

我需要将此数组作为图像源。

for image in self.iconArr {
            let img = image
            self.SlideShow.setImageInputs([ImageSource(image:  img)])
             }

但那不起作用,我怎么能这样做?

2 个答案:

答案 0 :(得分:4)

您应该尝试这种方式,因为您重置了for-loop

中的输入
var imageSource: [ImageSource] = []
for image in self.iconArr {
    let img = image
    imageSource.append(ImageSource(image:  img))
}
self.SlideShow.setImageInputs(imageSource)

正如sooper所说,可以这样做

let imageSources = self.iconArr.map { ImageSource(image: $0) } 

答案 1 :(得分:0)

我从这个网址[https://stackoverflow.com/a/50461970/5628693][1]

找到了一个解决方案

以下是我的代码正常工作:

 var imageSDWebImageSrc = [SDWebImageSource]()
@IBOutlet weak var slideshow: ImageSlideshow!

添加以下viewDidLoad()

 slideshow.backgroundColor = UIColor.white
    slideshow.slideshowInterval = 5.0
    slideshow.pageControlPosition = PageControlPosition.underScrollView
    slideshow.pageControl.currentPageIndicatorTintColor = UIColor.lightGray
    slideshow.pageControl.pageIndicatorTintColor = UIColor.black
    slideshow.contentScaleMode = UIViewContentMode.scaleAspectFill

    // optional way to show activity indicator during image load (skipping the line will show no activity indicator)
    slideshow.activityIndicator = DefaultActivityIndicator()
    slideshow.currentPageChanged = {
        page in
        print("current page:", page)
    }

    let recognizer = UITapGestureRecognizer(target: self, action: #selector(Dashboard.didTap))
    slideshow.addGestureRecognizer(recognizer)
} // now add below func 
@objc func didTap() {
    let fullScreenController = slideshow.presentFullScreenController(from: self)
    // set the activity indicator for full screen controller (skipping the line will show no activity indicator)
    fullScreenController.slideshow.activityIndicator = DefaultActivityIndicator(style: .white, color: nil)
}

最后一步我从alamofire请求下面获取json数据

  Alamofire.request(url, method: .post, parameters: data, encoding: JSONEncoding.default).responseJSON { response in
   if(response.value == nil){
        }
   else {
        let json2 = JSON(response.value!)
        switch response.result {
        case .success:
            self.indicator.stopAnimating()

            if let details = json2["imgs"].array {
                for dItem in details {

                   let  img = dItem["img"].stringValue

                   let image = SDWebImageSource(urlString: self.imgurl+img)
                    self.imageSDWebImageSrc.append(image!)
                }
             self.slideshow.setImageInputs(self.imageSDWebImageSrc)
            }
            break
        case .failure( _):

            break

        }
    }
    }

谢谢老兄:)快乐编码