Haneke(斯威夫特)无序缓存图像

时间:2016-09-30 08:00:25

标签: swift caching swifty-json haneke

我开发了一个菜单界面,它有3行六边形,可以动态加载,并允许用户在属于同一类别的不同服务中进行选择。然后,我调用了我的API端点以获取具有相关数据的JSON响应,这也正常工作。

问题:第一次打开菜单(即菜单是动态构建的),图像显示的顺序错误。如果我调用另一个视图然后返回相同的菜单,则所有内容都会正确显示。当然,这种行为非常烦人,但到目前为止,我解决这个问题的努力都没有成功。

这里我用于第一行六边形的代码片段(第二行和第三行)在此之后按顺序调用:

func readJSONforPacketsRow1(language:Int, category:Int, location:String){
print ("FIRST ROW")
Alamofire.request(.GET, "\(URL.base)packets/1,\(language),\(category),\(location)", parameters:nil,  encoding: ParameterEncoding.URL)
        .responseJSON { response in
            let json = JSON(data: response.data!)
            if let JSON = response.result.value {
                for packet in json.arrayValue


                {
                    let name = packet["name"].stringValue
                    let name_second = packet["name_second"].stringValue
                    let image = packet["image"].stringValue
                    let description = packet["description"].stringValue
                    let language = packet["language"].stringValue
                    let id = packet["id"].intValue
                    let packet_type = packet["packet_type"].intValue

                    let mypacket = Packet(id: id, packet_type: packet_type, name: name, name_second: name_second, description: description, image: image, language: language)

                    self.packetArray.append(mypacket)

                }


                if (self.packetArray.count > 0){
                //First row

                self.packetArray.sortInPlace{$0.id < $1.id}
                var buttonX: CGFloat = 60  // our Starting Offset, could be 0

                for i in 0..<self.packetArray.count {
                    self.indicator.startAnimating()


                    let URLc = NSURL(string: "\(URL.base)packetimages/\(self.packetArray[i].image)")!
                    let fetcher = NetworkFetcher<UIImage>(URL: URLc)
                    let cachedEntryKey = fetcher.key
                    self.cache.fetch(key:cachedEntryKey).onSuccess { image in
                        let imageData = UIImagePNGRepresentation(image)!
                        let cachedImage = UIImage(data: imageData, scale: 1)!
                        let button = UIButton(frame: CGRect(x: buttonX, y: 30, width: self.hexwidth, height: self.hexheight))
                        buttonX = buttonX + self.hexmargin
                        button.tintColor = UIColor.whiteColor()
                        button.backgroundColor = UIColor.clearColor()
                        button.setTitleColor(UIColor.blackColor(), forState: .Normal)
                        button.setImage(SliderImageButton.imageOfSliderButton(cachedImage, isSelected: false, text: "\(self.packetArray[i].name)", text2: "\(self.packetArray[i].name_second)"), forState: .Normal)
                        button.setImage(SliderImageButton.imageOfSliderButton(cachedImage, isSelected: true, text: "\(self.packetArray[i].name)", text2: "\(self.packetArray[i].name_second)"), forState: .Highlighted)
                        button.addTarget(self, action: #selector(PacketMenu.clickMe(_:)), forControlEvents: UIControlEvents.TouchUpInside)
                        button.tag = self.packetArray[i].id
                        self.view.addSubview(button)







                        }

                    }

                }
            }
    }
}

任何帮助将不胜感激!非常感谢撒丁岛!

0 个答案:

没有答案