Browse Source

fix downscaling of avatar images in chat title views

cyberta 5 years ago
parent
commit
f1d4e3c54a

+ 2 - 2
deltachat-ios/Helper/UIImage+Extension.swift

@@ -24,7 +24,7 @@ extension UIImage {
         self.init(cgImage: cgImage)
     }
 
-    func resizeImage(targetSize: CGSize) -> UIImage {
+    func resizeImage(targetSize: CGSize) -> UIImage? {
         let size = self.size
 
         let widthRatio  = targetSize.width  / size.width
@@ -44,6 +44,6 @@ extension UIImage {
         let newImage = UIGraphicsGetImageFromCurrentImageContext()
         UIGraphicsEndImageContext()
 
-        return newImage!
+        return newImage
     }
 }

+ 8 - 6
deltachat-ios/View/InitialsBadge.swift

@@ -35,9 +35,9 @@ class InitialsBadge: UIView {
     private func setupSubviews() {
         addSubview(label)
         label.translatesAutoresizingMaskIntoConstraints = false
-        label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 2).isActive = true
-        label.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -2).isActive = true
-        label.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0).isActive = true
+        label.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
+        label.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
+        label.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
     }
 
     required init?(coder _: NSCoder) {
@@ -49,9 +49,11 @@ class InitialsBadge: UIView {
     }
 
     func setImage(_ image: UIImage) {
-        let attachment = NSTextAttachment()
-        attachment.image = image
-        label.attributedText = NSAttributedString(attachment: attachment)
+        if let resizedImg = image.resizeImage(targetSize: CGSize(width: self.frame.width, height: self.frame.height)) {
+            let attachment = NSTextAttachment()
+            attachment.image = resizedImg
+            label.attributedText = NSAttributedString(attachment: attachment)
+        }
     }
 
     func setColor(_ color: UIColor) {