2017-06-13 73 views
0

我创建了一个列表视图,其中包含图像,当向下滚动时会自动请求新项目。我正在使用Image组件来显示来自url源的图像。问题在于图像在加载后不会缓存在内存中。当我向下滚动时,我可以看到它们,但当我回来时,我必须等待它们再次加载。有没有办法来解决这个问题?图像组件具有属性缓存但它没有任何改进。我知道在Android中,这是以完全相同的方式完成的,并且图像一旦下载就保存在内存中。 这里是一个示例代码:qml listview图像缓存不能按预期的那样工作

ApplicationWindow { 
    visible: true 
    width: 800 
    height: 600 
    id: rootId 

    property url fullImageUrl 
    property string tag : "windsurfing" 

    XmlListModel{ 
     id : modelId 
     namespaceDeclarations: "declare namespace media = 'http://search.yahoo.com/mrss/';" 
     source: "http://api.flickr.com/services/feeds/photos_public.gne?format=rss2&tags="+tag 
     query: "//item[title and media:thumbnail and media:content]" 
     XmlRole{name:"fullImage"; query:"media:content/@url/string()" } 
    } 

    TextField{ 
     id : userValueId 
     font.pointSize: 14 
     width: parent.width 
     height : implicitHeight 
     placeholderText: "Enter a Flickr Tag" 
     onEditingFinished: tag = text 
    } 

    ListView{ 
     id : listViewId 
     anchors.fill: parent 
     anchors.topMargin: userValueId.height + 10 

     Layout.minimumWidth: 400 
     Layout.maximumWidth: parent.width - 50 
     model: modelId 
     delegate: delegateId 
    } 

    Component{ 
     id: delegateId 
     Rectangle{ 
      id :itemId 
      height : 300 
      width : 500 
      Image{ 
       id : imageId 
       source : fullImage 
       anchors.fill: parent 
       fillMode: Image.Stretch 
       cache: true 
      } 
     } 
    } 
} 

回答

0

我如何可以缓存由模型提供,在 QML ListView中绘制图形的项目?

我会尝试使用以像素指定的ListView cacheBuffer属性来适应代表。如果您的委托在滚动方向上有300个像素(例如,高度,并且垂直滚动),那么每行有一个代表和“缓存缓冲区”为10000像素,则它最多适合33位代表。

ListView { 
    id : listViewId 
    anchors.fill: parent 
    anchors.topMargin: userValueId.height + 10 
    cacheBuffer: 10000 // pixels in direction of scrolling the view, 
         // saves a bit of processing just by caching 
         // delegates content in memory, causes async 
         // read-ahead for images outside of view area 
    Layout.minimumWidth: 400 
    Layout.maximumWidth: parent.width - 50 
    model: modelId 
    delegate: delegateId 
} 

如果内存的数量受到严格限制,指定相当小的cacheBuffer是有意义的,例如, 2页的列表视图,以防止太多的预读。缓存不能保证数据不会被再次读取。 我也有一些怀疑,如果使用组件是正确的方式与图像缓存,但总结认为不应该影响的东西,只要没有在该组件代码中的任何时间执行加载的加载程序。

而且你也可以尝试在C++中实现自己的image provider来显式控制图像下载/缓存,这样逻辑将完全由你的代码控制。

相关问题