Save artwork to jpg instead of png to reduce size.

CQTexperiment
Dzmitry Neviadomski 2021-01-07 08:45:44 +03:00
parent bd4e64c029
commit e16d4e8aa7
1 changed files with 12 additions and 7 deletions

View File

@ -177,26 +177,31 @@ didReceiveNotificationResponse:(UNNotificationResponse *)response
if ([defaults boolForKey:@"notifications.show-album-art"] && [pe albumArtInternal]) { if ([defaults boolForKey:@"notifications.show-album-art"] && [pe albumArtInternal]) {
NSError *error = nil; NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager]; NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *tmpSubFolderName = [NSProcessInfo.processInfo globallyUniqueString];
NSURL *tmpSubFolderURL = [[NSURL fileURLWithPath:NSTemporaryDirectory()] NSURL *tmpSubFolderURL = [[NSURL fileURLWithPath:NSTemporaryDirectory()]
URLByAppendingPathComponent:tmpSubFolderName isDirectory:true]; URLByAppendingPathComponent:@"cog-artworks-cache" isDirectory:true];
if ([fileManager createDirectoryAtPath:[tmpSubFolderURL path] if ([fileManager createDirectoryAtPath:[tmpSubFolderURL path]
withIntermediateDirectories:true withIntermediateDirectories:true
attributes:nil attributes:nil
error:&error]) { error:&error]) {
NSURL *fileURL = [tmpSubFolderURL URLByAppendingPathComponent:@"art.png"]; NSString *tmpFileName = [[NSProcessInfo.processInfo globallyUniqueString] stringByAppendingString:@".jpg"];
NSURL *fileURL = [tmpSubFolderURL URLByAppendingPathComponent:tmpFileName];
NSImage *image = [pe albumArt]; NSImage *image = [pe albumArt];
CGImageRef cgRef = [image CGImageForProposedRect:NULL context:nil hints:nil]; CGImageRef cgRef = [image CGImageForProposedRect:NULL context:nil hints:nil];
NSBitmapImageRep *newRep = [[NSBitmapImageRep alloc] initWithCGImage:cgRef]; NSBitmapImageRep *newRep = [[NSBitmapImageRep alloc] initWithCGImage:cgRef];
[newRep setSize:[image size]]; NSData *jpgData = [newRep representationUsingType:NSBitmapImageFileTypeJPEG
NSData *pngData = [newRep representationUsingType:NSBitmapImageFileTypePNG properties:@{}]; properties:@{NSImageCompressionFactor: @0.5f}];
[pngData writeToURL:fileURL atomically:YES]; [jpgData writeToURL:fileURL atomically:YES];
UNNotificationAttachment *icon = [UNNotificationAttachment attachmentWithIdentifier:@"art" UNNotificationAttachment *icon = [UNNotificationAttachment attachmentWithIdentifier:@"art"
URL:fileURL URL:fileURL
options:nil options:nil
error:&error]; error:&error];
content.attachments = @[icon]; if (error) {
// We have size limit of 10MB per image attachment.
NSLog(@"%@", error.localizedDescription);
} else {
content.attachments = @[icon];
}
} }
} }