我尝试过某种方式,但没有成功。比如:
- cell.ImageView.Image = UIImage.FromFile(tableItems [indexPath.Row].artwork_url);//artwork_url是网站上的图片。
- cell.ImageView.Image=UIImage.FromFile("/Photos/icon100x100.png");
- 我不在 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/