While working on my BB10 App update, I required to show Remote Image using ImageView BB10 cascades API. ImageView or Image component by default does not support loading image from URL on internet. I implemented a custom component which serve the purpose and also easily integrated with ImageView.
Following is implementation for the same, I hope it will be useful to someone.
Let's start by showing how my custom component works. Following code shows how to use ImageDownloader custom component along with ImageView to display Grid of images from internet. Data Model contains two kind of URL, url for low resolution image and url for high resolution image.
Following is implementation for the same, I hope it will be useful to someone.
Let's start by showing how my custom component works. Following code shows how to use ImageDownloader custom component along with ImageView to display Grid of images from internet. Data Model contains two kind of URL, url for low resolution image and url for high resolution image.
import my.library 1.0 ListView { id: listView layout: GridListLayout {} dataModel: model; listItemComponents: [ ListItemComponent { ImageView { id: imgView imageSource: "default_image.jpg" attachedObjects: [ ImageDownloader { id: imageLoader url: ListItemData.mediumImage onImageChanged: { if (imageLoader.isVaid) { imgView.image = imageLoader.image } } } ] } } ] }ImageDownloader component is available in QML because we imported "my.library". We can make any C++ code available to QML by registering it to QML System, folloiwing snippet shows how we can register C++ component to QML system.
#include "ImageDownloader.h" int main(int argc, char **argv) { Application app(argc, argv); qmlRegisterType<ImageDownloader>("my.library",1, 0, "ImageDownloader"); QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(&app); AbstractPane *root = qml->createRootObjectNow that custom component is ready to be used with QML, let's see how its implemented. Below if header file for ImageDownloader class. We are defining few properties like url, image and isValid. By setting "url" we can initiate download of image, when "image" download is finished downloaded image can be accessed by using "image" property. We can check if image is valid or not by checking "isValid" property. we are also defining few signal like "urlChanged" and "imageChanged", which are emited when url is changed or image is downloaded. And we are using QNetworkAccessManager to download image from internet.(); app.setScene(root); return Application::exec(); }
#ifndef IMAGEDOWNLOADER_H_ #define IMAGEDOWNLOADER_H_ #includeImplementation is also quite simple, let's see how its implemented. On URL change, we are initiating the image download using QNetworkAccessManager. When download is finished, we are reading image data in to buffer and creating Image using BB10 Image API.#include class QNetworkAccessManager; class ImageDownloader: public QObject { Q_OBJECT Q_PROPERTY(QVariant image READ image NOTIFY imageChanged) Q_PROPERTY(QString url READ url WRITE setUrl NOTIFY urlChanged) Q_PROPERTY(bool isVaid READ isValid CONSTANT); public: ImageDownloader( QObject* parent = 0); virtual ~ImageDownloader(); signals: void urlChanged(); void imageChanged(); private slots: QString url() const; void setUrl( const QString& url); QVariant image() const; bool isValid() const; void startDownload(); void onReplyFinished(); private: QNetworkAccessManager mNetManager; QString mImageUrl; bb::cascades::Image mImage; bool mIsValid; }; #endif /* IMAGEDOWNLOADER_H_ */
#include "ImageDownloader.h" #include <QNetworkAccessManager> #include <QNetworkRequest> #include <QNetworkReply> ImageDownloader::ImageDownloader(QObject* parent): QObject(parent), mIsValid(false) {} ImageDownloader::~ImageDownloader() {} QString ImageDownloader::url() const { return mImageUrl; } void ImageDownloader::setUrl( const QString& url) { if(url != mImageUrl) { mImageUrl = url; mIsValid = false; mImage = bb::cascades::Image(); emit urlChanged(); emit imageChanged(); startDownload(); } } QVariant ImageDownloader::image() const { return QVariant::fromValue(mImage); } bool ImageDownloader::isValid() const { return mIsValid; } void ImageDownloader::startDownload() { QNetworkRequest request(mImageUrl); QNetworkReply* reply = mNetManager.get(request); connect(reply, SIGNAL(finished()), this, SLOT(onReplyFinished())); } void ImageDownloader::onReplyFinished() { QNetworkReply* reply = qobject_castThat's all we have to do to do display remote image in BB10, hope you liked it.(sender()); QString response; if (reply) { if (reply->error() == QNetworkReply::NoError) { const int available = reply->bytesAvailable(); if (available > 0) { const QByteArray data(reply->readAll()); mImage = bb::cascades::Image(data); mIsValid = true; emit imageChanged(); } } reply->deleteLater(); } }
example use it with url
ReplyDeleteerror at bb::cascades::Image mImage;
Delete