Skip to content

Commit

Permalink
Fix: downscaling of custom icons (fixes #354)
Browse files Browse the repository at this point in the history
[thanks, ixs]
  • Loading branch information
keepassium committed Mar 27, 2024
1 parent 9ca4343 commit 47a6a72
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion KeePassium/database/entry/edit/EntryFieldEditorCells.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class EntryFieldEditorTitleCell:
guard let field else { return }
let buttonConfig = delegate?.getActionConfiguration(for: field)
buttonConfig?.apply(to: iconButton)
iconButton.configuration?.image = icon?.downscalingToSquare(maxSide: 29)
iconButton.configuration?.image = icon?.downscalingToSquare(maxSidePoints: 30)

titleTextField.text = field.value
}
Expand Down
2 changes: 1 addition & 1 deletion KeePassium/database/group/GroupEditorTitleCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ final class GroupEditorTitleCell: UITableViewCell {
didSet {
nameTextField.text = group?.name
let icon = (group != nil) ? UIImage.kpIcon(forGroup: group!) : nil
iconButton.configuration?.image = icon?.downscalingToSquare(maxSide: 29)
iconButton.configuration?.image = icon?.downscalingToSquare(maxSidePoints: 30)
delegate?.didChangeValidity(isValid: nameTextField.isValid, in: self)
}
}
Expand Down
2 changes: 1 addition & 1 deletion KeePassiumLib/KeePassiumLib/db/kp2/CustomIcon2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

public class CustomIcon2: Eraseable {
public static let maxSide = CGFloat(128)
public static let maxSidePixels = CGFloat(120)

public private(set) var uuid: UUID
public private(set) var data: ByteArray
Expand Down
2 changes: 1 addition & 1 deletion KeePassiumLib/KeePassiumLib/db/kp2/Database2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,7 @@ public class Database2: Database {
}

public func addCustomIcon(_ image: UIImage) -> CustomIcon2? {
guard let normalizedImage = image.downscalingToSquare(maxSide: CustomIcon2.maxSide) else {
guard let normalizedImage = image.downscalingToSquare(maxSidePixels: CustomIcon2.maxSidePixels) else {
Diag.error("Failed to normalize the image, cancelling")
return nil
}
Expand Down
12 changes: 8 additions & 4 deletions KeePassiumLib/KeePassiumLib/util/UIImage+downscaling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ import UIKit

public extension UIImage {

func downscalingToSquare(maxSide: CGFloat) -> UIImage? {
func downscalingToSquare(maxSidePixels: CGFloat) -> UIImage? {
return downscalingToSquare(maxSidePoints: maxSidePixels / UIScreen.main.scale)
}

func downscalingToSquare(maxSidePoints: CGFloat) -> UIImage? {
let targetSide: CGFloat
if size.width > maxSide && size.height > maxSide {
targetSide = maxSide
if size.width > maxSidePoints && size.height > maxSidePoints {
targetSide = maxSidePoints
} else {
targetSide = min(size.width, size.height)
}

let targetSize = CGSize(width: targetSide, height: targetSide)
UIGraphicsBeginImageContextWithOptions(targetSize, false, 0.0)
UIGraphicsBeginImageContextWithOptions(targetSize, false, UIScreen.main.scale)
self.draw(in: CGRect(x: 0, y: 0, width: targetSide, height: targetSide))
let resized = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Expand Down

0 comments on commit 47a6a72

Please sign in to comment.