同じ API を使用した迅速で汚れたアプローチ (私が持っているいくつかのコードから適応) は次のようになります。
// Show a "waiting / placeholder" image until the real one is available
image.Image = ...
UIImage img = null;
// Download the images outside the main (UI) thread
ThreadPool.QueueUserWorkItem (delegate {
UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
using (NSUrl nsurl = new NSUrl (url))
using (NSData data = NSData.FromUrl (nsurl)) {
UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
// we might not get any data, e.g. if there's no internet connection available
if (data != null)
img = UIImage.LoadFromData (data);
}
// finally call the completion action on the main thread
NSRunLoop.Main.InvokeOnMainThread (delegate {
// note: if `img` is null then you might want to keep the "waiting"
// image or show a different one
image.Image = img;
});
});
もう 1 つの方法として、ダウンロードした画像をキャッシュすることもできますが (オフライン モードなど)、アプリケーションによってはそれができない場合があります。
そのような場合、最初にキャッシュにイメージがあるかどうかを確認し、そうでない場合は、ダウンロード後に保存することを確認します (すべてバックグラウンド スレッドで行われます)。