2015-04-02 47 views

回答

5

经过多一点研究后,我发现这个样本https://github.com/telerik/nativescript-sample-cuteness/blob/master/nativescript-sample-cuteness/,它有大量从互联网下载的图像。

我使用的是一个名为的模块,image-cache可以解决这个问题。

这是我用更精确:

var imageSourceModule = require("image-source"); 
var imageCache = require("ui/image-cache"); 

var cache = new imageCache.Cache(); 
var defaultImageSource = imageSourceModule.fromFile("~/app/res/loading.gif"); 
var defaultNotFoundImageSource = imageSourceModule.fromFile("~/app/res/no-image.png"); 

cache.invalid = defaultNotFoundImageSource; 
cache.placeholder = defaultImageSource; 
cache.maxRequests = 5; 

function displayImage(viewModel, url, propertyName) { 
    var source = cache.get(url); 

    propertyName = propertyName || "image"; 

    if (source) { 
    viewModel.set(propertyName, source); 
    } else { 
    viewModel.set(propertyName, defaultImageSource); 
    cache.push({ 
     key: url, 
     url: url, 
     completed: function (result, key) { 
     if (key === url) { 
      viewModel.set(propertyName, result); 
     } 
     } 
    }); 
    } 
} 

如果有一个更好的解决方案,我会很乐意学习一下吧。

+0

这对我有效。 – 2015-04-02 20:28:54

+0

那么你如何在视图@miroslav上展示它? – 2016-05-13 03:27:27

相关问题