Commit 43a79239 authored by Kateryna Kostiuk's avatar Kateryna Kostiuk Committed by Andreas Traczyk

fix: image memory usage

Change-Id: I8ba722d89c9a54ec20d25a3d2b7dd3efb20b1dd0
parent 394f74c7
...@@ -137,15 +137,12 @@ typedef NS_ENUM(NSInteger, TagViews) { ...@@ -137,15 +137,12 @@ typedef NS_ENUM(NSInteger, TagViews) {
-(void) updateView { -(void) updateView {
const auto& account = accountModel->getAccountInfo(self.selectedAccountID); const auto& account = accountModel->getAccountInfo(self.selectedAccountID);
QByteArray ba = QByteArray::fromStdString(account.profileInfo.avatar);
QVariant photo = GlobalInstances::pixmapManipulator().personPhoto(ba, nil); NSData *imageData = [[NSData alloc] initWithBase64EncodedString:@(account.profileInfo.avatar.c_str()) options:NSDataBase64DecodingIgnoreUnknownCharacters];
if(QtMac::toNSImage(qvariant_cast<QPixmap>(photo))) { NSImage *image = [[NSImage alloc] initWithData:imageData];
if(image) {
[photoView setBordered:NO]; [photoView setBordered:NO];
NSImage *image = QtMac::toNSImage(qvariant_cast<QPixmap>(photo)); [photoView setImage: [image roundCorners: 350]];
CGFloat newSize = MIN(image.size.height, image.size.width);
image = [image cropImageToSize:CGSizeMake(newSize, newSize)];
[photoView setImage: [image roundCorners: image.size.height * 0.5]];
[addProfilePhotoImage setHidden:YES]; [addProfilePhotoImage setHidden:YES];
} else { } else {
[photoView setImage:nil]; [photoView setImage:nil];
......
...@@ -125,15 +125,11 @@ typedef NS_ENUM(NSInteger, TagViews) { ...@@ -125,15 +125,11 @@ typedef NS_ENUM(NSInteger, TagViews) {
-(void)updateView { -(void)updateView {
const auto& account = accountModel->getAccountInfo(self.selectedAccountID); const auto& account = accountModel->getAccountInfo(self.selectedAccountID);
QByteArray ba = QByteArray::fromStdString(account.profileInfo.avatar); NSData *imageData = [[NSData alloc] initWithBase64EncodedString:@(account.profileInfo.avatar.c_str()) options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSImage *image = [[NSImage alloc] initWithData:imageData];
QVariant photo = GlobalInstances::pixmapManipulator().personPhoto(ba, nil); if(image) {
if(QtMac::toNSImage(qvariant_cast<QPixmap>(photo))) {
[photoView setBordered:NO]; [photoView setBordered:NO];
NSImage *image = QtMac::toNSImage(qvariant_cast<QPixmap>(photo)); [photoView setImage: [image roundCorners: 350]];
CGFloat newSize = MIN(image.size.height, image.size.width);
image = [image cropImageToSize:CGSizeMake(newSize, newSize)];
[photoView setImage: [image roundCorners: image.size.height * 0.5]];
[addProfilePhotoImage setHidden:YES]; [addProfilePhotoImage setHidden:YES];
} else { } else {
[photoView setImage:nil]; [photoView setImage:nil];
......
...@@ -122,7 +122,11 @@ NSMenuItem* selectedMenuItem; ...@@ -122,7 +122,11 @@ NSMenuItem* selectedMenuItem;
QObject::connect(accMdl_, QObject::connect(accMdl_,
&lrc::api::NewAccountModel::accountStatusChanged, &lrc::api::NewAccountModel::accountStatusChanged,
[self] (const std::string& accountID) { [self] (const std::string& accountID) {
[self update]; if([self selectedAccount].id == accountID) {
[self update];
return;
}
[self updateMenuItemForAccount:accountID];
}); });
} }
...@@ -148,6 +152,16 @@ NSMenuItem* selectedMenuItem; ...@@ -148,6 +152,16 @@ NSMenuItem* selectedMenuItem;
} }
} }
-(void) updateMenuItemForAccount: (const std::string&) accountID {
AccountMenuItemView *itemView =
[accountsMenu itemWithTag:[@(accountID.c_str()) intValue]].view;
if(!itemView) {
return;
}
[self configureView:itemView forAccount:accountID];
}
-(void) updateMenu { -(void) updateMenu {
[accountsMenu removeAllItems]; [accountsMenu removeAllItems];
...@@ -195,10 +209,10 @@ NSMenuItem* selectedMenuItem; ...@@ -195,10 +209,10 @@ NSMenuItem* selectedMenuItem;
[itemView.accountLabel setStringValue:@(account.profileInfo.alias.c_str())]; [itemView.accountLabel setStringValue:@(account.profileInfo.alias.c_str())];
NSString* userNameString = [self nameForAccount: account]; NSString* userNameString = [self nameForAccount: account];
[itemView.userNameLabel setStringValue:userNameString]; [itemView.userNameLabel setStringValue:userNameString];
QByteArray ba = QByteArray::fromStdString(account.profileInfo.avatar); NSData *imageData = [[NSData alloc] initWithBase64EncodedString:@(account.profileInfo.avatar.c_str()) options:NSDataBase64DecodingIgnoreUnknownCharacters];
QVariant photo = GlobalInstances::pixmapManipulator().personPhoto(ba, nil); NSImage *image = [[NSImage alloc] initWithData:imageData];
if(QtMac::toNSImage(qvariant_cast<QPixmap>(photo))) { if(image) {
[itemView.accountAvatar setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))]; [itemView.accountAvatar setImage: image];
} else { } else {
[itemView.accountAvatar setImage: [NSImage imageNamed:@"default_avatar_overlay.png"]]; [itemView.accountAvatar setImage: [NSImage imageNamed:@"default_avatar_overlay.png"]];
} }
...@@ -229,12 +243,10 @@ NSMenuItem* selectedMenuItem; ...@@ -229,12 +243,10 @@ NSMenuItem* selectedMenuItem;
auto& account = [self selectedAccount]; auto& account = [self selectedAccount];
if(account.profileInfo.type == lrc::api::profile::Type::INVALID) if(account.profileInfo.type == lrc::api::profile::Type::INVALID)
return; return;
NSData *imageData = [[NSData alloc] initWithBase64EncodedString:@(account.profileInfo.avatar.c_str()) options:NSDataBase64DecodingIgnoreUnknownCharacters];
QByteArray ba = QByteArray::fromStdString(account.profileInfo.avatar); NSImage *image = [[NSImage alloc] initWithData:imageData];
if(image) {
QVariant photo = GlobalInstances::pixmapManipulator().personPhoto(ba, nil); [profileImage setImage: image];
if(QtMac::toNSImage(qvariant_cast<QPixmap>(photo))) {
[profileImage setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))];
} else { } else {
[profileImage setImage: [NSImage imageNamed:@"default_avatar_overlay.png"]]; [profileImage setImage: [NSImage imageNamed:@"default_avatar_overlay.png"]];
} }
......
...@@ -42,8 +42,7 @@ ...@@ -42,8 +42,7 @@
} }
- (NSImage *) roundCorners:(CGFloat)radius { - (NSImage *) roundCorners:(CGFloat)radius {
NSImage *existingImage = self; NSSize existingSize = [self size];
NSSize existingSize = [existingImage size];
NSSize newSize = NSMakeSize(existingSize.width, existingSize.height); NSSize newSize = NSMakeSize(existingSize.width, existingSize.height);
NSImage *composedImage = [[NSImage alloc] initWithSize:newSize]; NSImage *composedImage = [[NSImage alloc] initWithSize:newSize];
...@@ -55,7 +54,7 @@ ...@@ -55,7 +54,7 @@
[clipPath setWindingRule:NSEvenOddWindingRule]; [clipPath setWindingRule:NSEvenOddWindingRule];
[clipPath addClip]; [clipPath addClip];
[existingImage drawAtPoint:NSZeroPoint fromRect:NSMakeRect(0, 0, newSize.width, newSize.height) operation:NSCompositeSourceOver fraction:1]; [self drawAtPoint:NSZeroPoint fromRect:NSMakeRect(0, 0, newSize.width, newSize.height) operation:NSCompositeSourceOver fraction:1];
[composedImage unlockFocus]; [composedImage unlockFocus];
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment