使用Alamofire下载图像并在UITableViewCell

时间:2017-05-18 05:27:26

标签: ios json uitableview swift3 alamofire

我有这两个功能:

func callAlamo(url: String){
    Alamofire.request(url).responseJSON(completionHandler: {
        response in

        self.parseData(JSONData: response.data!)

    })
}

func parseData(JSONData: Data) {
    do {
        var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONStandard
        if let tracks = readableJSON["tracks"] as? JSONStandard{
            if let items = tracks["items"] as? [JSONStandard]{
                for i in 0..<items.count{
                    let item = items[i]
                    let name = item["name"] as! String

                    if let album = item["album"] as? JSONStandard{
                        if let images = album["images"] as? [JSONStandard]{
                            let imageData = images[0]
                            let mainImageUrl = URL(string: imageData["url"] as! String)
                            let mainImageData = NSData(contentsOf: mainImageUrl!)

                            let mainImage = UIImage(data: mainImageData as! Data)
                            posts.append(post.init(image: mainImage, name: name))

                            self.tableView.reloadData()
                        }
                    }
                }
            }
        }
    }
    catch{
        print(error)
    }
}

使用alamofire从Spotify检索歌曲信息。这些函数在UITableViewController类中设置。我在设置此图像的同一文件中设置了一个单独的UITableViewCell类:

let albumCoverImageView: UIImageView = {
    let imageView = UIImageView()
    imageView.translatesAutoresizingMaskIntoConstraints = false

    return imageView
}()

我如何使用从两个函数中获取检索到的信息到该图像中?因为我无法从图像模仿内部访问这些功能。

2 个答案:

答案 0 :(得分:0)

使用“SDWebImage”库异步下载图像。

请参阅以下链接: -

https://github.com/rs/SDWebImage

用法: -

目标-C:

#import <SDWebImage/UIImageView+WebCache.h>
[imageView sd_setImageWithURL:[NSURL 
URLWithString:imageURL]
         placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

夫特:

import SDWebImage

imageView.sd_setImage(with: URL(string: imageURL), placeholderImage: UIImage(named: "placeholder.png"))

答案 1 :(得分:0)

var imgFinal = [String:UIImage]()

将图像存储在func parseData()中的“imgFinal”数组中:

self.imgArrList[i] = imageData["url"]


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


  let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CategoryTableViewCell
    load_image((imgArrList[indexPath.row] as? String)!, imageview: cell.titleImage, indVal: indexPath.row)
return cell
}

添加此方法以下载图像:

func load_image(link:String, imageview:UIImageView, indVal:Int)
{

    let url:NSURL = NSURL(string: link)!
    let session = NSURLSession.sharedSession()

    let request = NSMutableURLRequest(URL: url)
    request.timeoutInterval = 10

    let task = session.dataTaskWithRequest(request) {
        (
        let data, let response, let error) in

        guard let _:NSData = data, let _:NSURLResponse = response  where error == nil else {

            return
        }

        var image = UIImage(data: data!)

        if (image != nil)
        {
            func set_image()
            {
                //self.images_cache[link] = image
                self.imgFinal[link] = image
                imageview.image = image
            }
            dispatch_async(dispatch_get_main_queue(), set_image)
        }
    }
    task.resume()
}