images: cache default photo
It takes up to 40% of memory when app is loading
This image is now cached in a QHash with a key computed from the size
asked.
Change-Id: Iebb14e9c38dbd6e9057c9fbe5a366542fefffca5
Tuleap: #663
diff --git a/src/delegates/ImageManipulationDelegate.h b/src/delegates/ImageManipulationDelegate.h
index 43e7446..9605ba3 100644
--- a/src/delegates/ImageManipulationDelegate.h
+++ b/src/delegates/ImageManipulationDelegate.h
@@ -21,13 +21,14 @@
//Qt
#import <QSize>
+#import <QtGui/qpixmap.h>
//Ring
#import <interfaces/pixmapmanipulatori.h>
#import <call.h>
class Person;
-class QPixmap;
+class QString;
namespace Interfaces {
@@ -61,6 +62,8 @@
QPixmap drawDefaultUserPixmap(const QSize& size, bool displayPresence = false, bool isPresent = false);
CGImageRef resizeCGImage(CGImageRef image, const QSize& size);
+ QHash<QString, QPixmap> m_hDefaultUserPixmap;
+
/**
* Return a version of size destSize centered of the bigger photo
*/
diff --git a/src/delegates/ImageManipulationDelegate.mm b/src/delegates/ImageManipulationDelegate.mm
index c21f8e4..7780d94 100644
--- a/src/delegates/ImageManipulationDelegate.mm
+++ b/src/delegates/ImageManipulationDelegate.mm
@@ -26,6 +26,7 @@
#import <QBuffer>
#import <QtGui/QColor>
#import <QtGui/QPainter>
+#import <QHash>
#import <QtGui/QBitmap>
#import <QtWidgets/QApplication>
#import <QtGui/QImage>
@@ -40,10 +41,7 @@
namespace Interfaces {
- ImageManipulationDelegate::ImageManipulationDelegate()
- {
-
- }
+ ImageManipulationDelegate::ImageManipulationDelegate() {}
QVariant ImageManipulationDelegate::contactPhoto(Person* c, const QSize& size, bool displayPresence) {
const int radius = size.height() / 2;
@@ -164,6 +162,12 @@
}
QPixmap ImageManipulationDelegate::drawDefaultUserPixmap(const QSize& size, bool displayPresence, bool isPresent) {
+
+ auto index = QStringLiteral("%1%2").arg(size.width()).arg(size.height());
+ if (m_hDefaultUserPixmap.contains(index)) {
+ return m_hDefaultUserPixmap.value(index);
+ }
+
// create the image somehow, load from file, draw into it...
auto sourceImgRef = CGImageSourceCreateWithData((__bridge CFDataRef)[[NSImage imageNamed:@"default_user_icon"] TIFFRepresentation], NULL);
auto imgRef = CGImageSourceCreateImageAtIndex(sourceImgRef, 0, NULL);
@@ -171,6 +175,8 @@
CFRelease(sourceImgRef);
CFRelease(imgRef);
+ m_hDefaultUserPixmap.insert(index, finalpxm);
+
return finalpxm;
}