将图像传递给pageView

时间:2018-06-01 15:24:26

标签: ios swift alamofire sdwebimage

我有一个我正在研究的iOS应用程序,我正在与Alamofire进行API调用,一切正常但我有一个问题我几天都无法解决,经过多次研究我无法得到有形的东西。所以我进行API调用并获取产品名称和图像和描述等...现在我使用SDWebImage将我的图像传递到表格视图,但图像可能大约为5但是对于着陆页我只需要索引1处的图像我能成功做到。我现在面临的问题是,当选择产品时,我将产品ID传递给变量,然后使用它来获取产品和其他属性的相应详细信息。

我能够完美地传递这些数据但是当涉及到传递图像时,我感到困惑,因为我想要一个像分页一样的分页来显示特定图像的所有图像。我能够实现pageview来获取硬编码图像的静态图像,其中涉及创建一个wi包含ImageView的uiviewcontroller,并将UIViewController以编程方式嵌入到网页视图中,如

lazy var controllers: [UIViewController] = {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        var controllers = [UIViewController]()

        if let images = self.images {
            for image in images {
                let shoeImageVC = storyboard.instantiateViewController(withIdentifier: "xxxxx")
                controllers.append(imageVC)
            }
        }

        self.pageViewControllerDelegate?.setupPageController(numberOfPages: controllers.count)

        return controllers
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        automaticallyAdjustsScrollViewInsets = false
        dataSource = self
        delegate = self

        self.turnToPage(index: 0)
    }

    func turnToPage(index: Int)
    {
        let controller = controllers[index]
        var direction = UIPageViewControllerNavigationDirection.forward

        if let currentVC = viewControllers?.first {
            let currentIndex = controllers.index(of: currentVC)!
            if currentIndex > index {
                direction = .reverse
            }
        }

        self.configureDisplaying(viewController: controller)

        setViewControllers([controller], direction: direction, animated: true, completion: nil)
    }

    func configureDisplaying(viewController: UIViewController)
    {
        for (index, vc) in controllers.enumerated() {
            if viewController === vc {
                if let imageVC = viewController as? ImageViewController {
                    imageVC.image = self.images?[index]

                    self.pageViewControllerDelegate?.turnPageController(to: index)
                }
            }
        }
    }
}

extension ImagesPageViewController : UIPageViewControllerDataSource
{
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
    {
        if let index = controllers.index(of: viewController) {
            if index > 0 {
                return controllers[index-1]
            }
        }

        return controllers.last
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
    {
        if let index = controllers.index(of: viewController) {
            if index < controllers.count - 1 {
                return controllers[index + 1]
            }
        }

        return controllers.first
    }
}

extension ImagesPageViewController : UIPageViewControllerDelegate
{
    func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController])
    {
        self.configureDisplaying(viewController: pendingViewControllers.first as! ImageViewController)
    }

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool)
    {
        if !completed {
            self.configureDisplaying(viewController: previousViewControllers.first as! ImageViewController)
        }
    }
}

但是从API调用中获取它们并使用SDWebImages进行渲染非常具有挑战性。我将产品详细信息传递给tableview单元格,并希望在我嵌入tableview的同一视图控制器中嵌入的页面视图以显示详细信息。嵌入式网页浏览控制器用于显示特定选定产品的图像。这是我传递单个产品的代码

var selectedProduct : Products?
......

func findIndividualProducts(id: Int, completion: @escaping CompletionHandler) -> Void {


        Alamofire.request("\(URL_PRODUCT_DETAIL)\(id)", method: .get, parameters: nil, encoding: JSONEncoding.default, headers: HEADER).responseJSON { (response) in

            if response.result.error == nil {

                do {
                    if let data = response.result.value {

                        let item = JSON(data)


                            let url = item["url"].stringValue
                            let id = item["id"].intValue
                            let title = item["title"].stringValue
                            let description = item["description"].stringValue
                            let recommendedProducts = item["recommended_products"].stringValue
                            let attributes = item["attributes"].arrayValue
                            let categories = item["categories"].arrayValue
                            let productClass = item["product_class"].stringValue

                        let imagesList = item["images"].arrayValue

                        let imagesURL = imagesList.map {$0["original"].stringValue}
                            let options = item["options"].stringValue
                            let children = item["children"].stringValue


                            let productDetail = ProductDetail(productUrl: url, id: id, productTitle: title, productDescription: description, recommendedProducts: recommendedProducts, attributes: attributes, categories: categories, productClass: productClass, productImg: imagesURL, options: options, children: children)

                            self.productDetails.append(productDetail)
                        completion(true)

                    }

                } catch {

                }
            } else {

            }
        }
    }

将所选产品详细信息传递到tableview单元格

func configureCell() -> Void {

        guard let productId = ProductServices.instance.selectedProduct?.id else {return}
        ProductServices.instance.findIndividualProducts(id: productId) { (success) in

            if success {

                ProductServices.instance.productDetails.forEach({ (productDetail) in
                    self.productTitle.text = productDetail.productTitle
                    self.productDescription.text = productDetail.productDescription
                    let htmlString = self.productDescription.text
                    let data = htmlString?.data(using: String.Encoding.unicode)! // mind "!"
                    let attrStr = try? NSAttributedString( // do catch
                        data: data!,
                        options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
                        documentAttributes: nil)
                    self.productDescription.attributedText = attrStr

                })
                print(ProductServices.instance.productDetails)
            }
        }

    }

ProductDetails模型

struct ProductDetail {

    public private(set) var productUrl : String!
    public private(set) var id : Int!
    public private(set) var productTitle : String!
    public private(set) var productDescription : String!
    public private(set) var recommendedProducts : String!
    public private(set) var attributes : Array<Any>!
    public private(set) var categories : Array<Any>!
    public private(set) var productClass : String!
    public private(set) var productImg : [String?]
    public private(set) var options : String!
    public private(set) var children : String!

}

可根据要求提供更多代码。谢谢提前

0 个答案:

没有答案