1

私のアプリでは、いくつかの画像をダウンロードする必要があります。画像は60kbトップです。

私がこれについて行ってきた方法は次のとおりです。

image.Image = UIImage.LoadFromData (NSData.FromUrl (new NSUrl("http://url")));

これは機能しますが、イメージのダウンロードに予想外に時間がかかります。私が知らない、これを行うためのより良い/より速い方法はありますか?

画像のダウンロードは、ViewDidLoad ()このビュー コントローラにセグエする前に不快なほど長い一時停止をもたらす方法で行われます。

4

2 に答える 2

2

MonoTouch.Dialog には、イメージのバックグラウンド ロードを処理するImageLoader (5.3 節) クラスが含まれており、「実際の」イメージのダウンロード中にデフォルト イメージを割り当てることができます。必ずしも高速であるとは限りませんが、ユーザーへの応答性が向上するはずです。

image.Image = ImageLoader.DefaultRequestImage( new Uri(uriString), this)

@poupou が指摘しているように、2 番目の引数は IImageUpdated を実装するクラスへの参照である必要があります。

于 2013-05-23T01:12:43.640 に答える
1

同じ 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 つの方法として、ダウンロードした画像をキャッシュすることもできますが (オフライン モードなど)、アプリケーションによってはそれができない場合があります。

そのような場合、最初にキャッシュにイメージがあるかどうかを確認し、そうでない場合は、ダウンロード後に保存することを確認します (すべてバックグラウンド スレッドで行われます)。

于 2013-05-23T01:44:16.137 に答える