ios - 如何在Xamarin studio上的tableview上显示图像?

标签 ios uitableview xamarin xamarin.ios

我尝试过某种方式,但没有成功。比如:

  1. cell.ImageView.Image = UIImage.FromFile(tableItems [indexPath.Row].artwork_url);//artwork_url是网站上的图片。
  2. cell.ImageView.Image=UIImage.FromFile("/Photos/icon100x100.png");
  3. 我不在 xamarin studio 上通过 nuget 安装 SDKWebimage。它的版本是 1.0.0。

以下所有方法均未运行。 TableView只显示cell.TextLabel.Text = tableItems [indexPath.Row].title;。 那不是图像。

我想显示一个图像: ell.ImageView.Image = UIImage.FromFile(tableItems [indexPath.Row].artwork_url);//artwork_url是网站上的图像。

请有人修复它。我是 ios 版 xamarin studio 的新手。

public async override Task<UITableViewCell> GetCell ( UITableView tableview, Foundation.NSIndexPath indexPath)
    {
        UITableViewCell cell = tableview.DequeueReusableCell (cellIdentifier);

        if (cell != null) {
            cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);
        } else {

            cell.ImageView.Image= await this.LoadImage(tableItems[indexPath.Row].artwork_url);
            cell.TextLabel.Text=tableItems[indexPath.Row].title;

        }
        return cell;
    }

最佳答案

你应该有一个异步方法来在显示之前先下载图像

public async Task<UIImage> LoadImage (string imageUrl)
    {
        var httpClient = new HttpClient();

        Task<byte[]> contentsTask = httpClient.GetByteArrayAsync (imageUrl);

        // await! control returns to the caller and the task continues to run on another thread
        var contents = await contentsTask;

        // load from bytes
        return UIImage.LoadFromData (NSData.FromArray (contents));
    }

然后你加载图像

LoadImage (tableItems [indexPath.Row].artwork_url).ContinueWith((task) => InvokeOnMainThread(() =>
    {
        cell.ImageView.Image = task.Result;
    }));

关于ios - 如何在Xamarin studio上的tableview上显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31622896/

相关文章:

ios - UITableView 中的 1 个 UISwitch 用于 4 个单元格

ios - 正在使用performSelector :afterDelay: the most efficient way to queue or order methods?

当按下新的 View Controller 时,iOS 8 UITableView 自调整单元格在视觉上跳跃/移动

sqlite - 如何在 Xamarin Forms 的 SQLite 中保存图像?

没有动画的 Xamarin 表单导航

ios - 重新加载时停止透明 UITableView 部分标题闪烁

c# - 在滚动表格 View 之前图像不会加载

iphone - UIScrollView 在初始缩放后滚动太远

ios - 在钥匙串(keychain)中找不到有效的 iPhone 代码签名 key

ios - 使用缓存实现 PFQueryTableViewController