From e16d4e8aa781990d62f23dec4e1b0ba26b03ac6c Mon Sep 17 00:00:00 2001 From: Dzmitry Neviadomski Date: Thu, 7 Jan 2021 08:45:44 +0300 Subject: [PATCH] Save artwork to jpg instead of png to reduce size. --- Application/PlaybackEventController.m | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/Application/PlaybackEventController.m b/Application/PlaybackEventController.m index 06c0e79a9..e0c56a387 100644 --- a/Application/PlaybackEventController.m +++ b/Application/PlaybackEventController.m @@ -177,26 +177,31 @@ didReceiveNotificationResponse:(UNNotificationResponse *)response if ([defaults boolForKey:@"notifications.show-album-art"] && [pe albumArtInternal]) { NSError *error = nil; NSFileManager *fileManager = [NSFileManager defaultManager]; - NSString *tmpSubFolderName = [NSProcessInfo.processInfo globallyUniqueString]; NSURL *tmpSubFolderURL = [[NSURL fileURLWithPath:NSTemporaryDirectory()] - URLByAppendingPathComponent:tmpSubFolderName isDirectory:true]; + URLByAppendingPathComponent:@"cog-artworks-cache" isDirectory:true]; if ([fileManager createDirectoryAtPath:[tmpSubFolderURL path] withIntermediateDirectories:true attributes:nil error:&error]) { - NSURL *fileURL = [tmpSubFolderURL URLByAppendingPathComponent:@"art.png"]; + NSString *tmpFileName = [[NSProcessInfo.processInfo globallyUniqueString] stringByAppendingString:@".jpg"]; + NSURL *fileURL = [tmpSubFolderURL URLByAppendingPathComponent:tmpFileName]; NSImage *image = [pe albumArt]; CGImageRef cgRef = [image CGImageForProposedRect:NULL context:nil hints:nil]; NSBitmapImageRep *newRep = [[NSBitmapImageRep alloc] initWithCGImage:cgRef]; - [newRep setSize:[image size]]; - NSData *pngData = [newRep representationUsingType:NSBitmapImageFileTypePNG properties:@{}]; - [pngData writeToURL:fileURL atomically:YES]; + NSData *jpgData = [newRep representationUsingType:NSBitmapImageFileTypeJPEG + properties:@{NSImageCompressionFactor: @0.5f}]; + [jpgData writeToURL:fileURL atomically:YES]; UNNotificationAttachment *icon = [UNNotificationAttachment attachmentWithIdentifier:@"art" URL:fileURL options:nil error:&error]; - content.attachments = @[icon]; + if (error) { + // We have size limit of 10MB per image attachment. + NSLog(@"%@", error.localizedDescription); + } else { + content.attachments = @[icon]; + } } }