将图像从S3存储桶加载到uiTableView

时间:2017-12-11 22:20:58

标签: ios swift uitableview amazon-s3

我有一个表视图,可以从s3存储桶加载图像,并使用我单元格中的图像设置一些数据。

我打电话给我的手机

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellDish:DishTableViewCell = tableView.dequeueReusableCell(withIdentifier: "DishCell", for: indexPath) as! DishTableViewCell

    cellDish.setDish(dish: brain.listOfDishes[indexPath.row])


    return cellDish }

在我的tableviewcell中我有一个名为setDish的函数:

func setDish (dish: Dish)
{
    var StringPrice = dish.DishPrice
    StringPrice.append("$")
    self.la_name.text = dish.DishName
    self.la_price.text = StringPrice
    self.la_des.text = dish.DishDes
    self.downloadData(dish: dish, completion: { success in
        guard success else { return }
        DispatchQueue.main.async {     
            self.dish_image.image = UIImage(data: dish.DishData!)!

        }
    })


}


func downloadData(dish:Dish,completion: @escaping (Bool) -> Void) {
    let transferUtility = AWSS3TransferUtility.default()
    let expression = AWSS3TransferUtilityDownloadExpression()
    let s3Bucket = "<my bucket name>"

    transferUtility.downloadData(fromBucket: s3Bucket, key: dish.DishImage, expression: expression) {(task, url, data, error) in
        if error != nil {print(error)
            completion(false)
        }
        else {

            dish.DishData = data!

        }
        completion(true)
    }

}

我希望它在没有图像的情况下向我显示菜单数据,直到图像下载然后显示给我(我希望它不在主线程上)。 我不知道为什么,但现在所有的细胞都下载了他们的图像,然后一切都加起来。

1 个答案:

答案 0 :(得分:0)

Swift 3更新代码: 加载url异步将自动更新

 Document doc = new Document();
     DocumentBuilder builder = new DocumentBuilder(doc);     
     Aspose.Words.Style style = doc.Styles.Add(StyleType.Paragraph, "newStyle");
     style.IsQuickStyle = true;
     style.Font.Size = 24;
      style.Font.Name = "B Mitra";


 builder.ParagraphFormat.Style = style;  
    builder.Writeln("سلام");

Swift 2.2代码:

extension UIImageView 
{
public func imageFromServerURL(urlString: String) 
{

        URLSession.shared.dataTask(with: NSURL(string: urlString)! as URL, completionHandler: { (data, response, error) -> Void in

            if error != nil {
                print(error)
                return
            }
            DispatchQueue.main.async(execute: { () -> Void in
                let image = UIImage(data: data!)
                self.image = image
            })

        }).resume()
    }}