瀏覽代碼

Merge pull request #979 from deltachat/tweak_image_message_layout

tweak aspect ratio for image messages:
cyBerta 4 年之前
父節點
當前提交
46dd1cf467
共有 2 個文件被更改,包括 38 次插入8 次删除
  1. 5 1
      deltachat-ios/Chat/ChatViewController.swift
  2. 33 7
      deltachat-ios/Chat/Views/Cells/ImageTextCell.swift

+ 5 - 1
deltachat-ios/Chat/ChatViewController.swift

@@ -308,13 +308,17 @@ class ChatViewController: UITableViewController {
                 if self.showCustomNavBar {
                     self.navigationItem.setRightBarButton(self.badgeItem, animated: true)
                 }
+                if lastSectionVisibleBeforeTransition {
+                    self.scrollToBottom(animated: false)
+                }
             },
             completion: {[weak self] _ in
                 guard let self = self else { return }
                 self.updateTitle(chat: self.dcContext.getChat(chatId: self.chatId))
                 if lastSectionVisibleBeforeTransition {
                     DispatchQueue.main.async { [weak self] in
-                        self?.scrollToBottom(animated: true)
+                        self?.tableView.reloadData()
+                        self?.scrollToBottom(animated: false)
                     }
                 }
             }

+ 33 - 7
deltachat-ios/Chat/Views/Cells/ImageTextCell.swift

@@ -35,8 +35,8 @@ class ImageTextCell: BaseMessageCell {
         mainContentView.addArrangedSubview(messageLabel)
         messageLabel.paddingLeading = 12
         messageLabel.paddingTrailing = 12
-        contentImageView.constraintAlignLeadingMaxTo(mainContentView).isActive = true
-        contentImageView.constraintAlignTrailingMaxTo(mainContentView).isActive = true
+        contentImageView.constraintAlignLeadingMaxTo(mainContentView, priority: .required).isActive = true
+        contentImageView.constraintAlignTrailingMaxTo(mainContentView, priority: .required).isActive = true
         topCompactView = true
         let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(onImageTapped))
         gestureRecognizer.numberOfTapsRequired = 1
@@ -101,13 +101,38 @@ class ImageTextCell: BaseMessageCell {
         if height == 0 || width == 0 {
             return
         }
+
+        let orientation = UIApplication.shared.statusBarOrientation
         self.imageHeightConstraint?.isActive = false
         self.imageWidthConstraint?.isActive = false
-        self.imageWidthConstraint = self.contentImageView.widthAnchor.constraint(lessThanOrEqualToConstant: width)
-        self.imageHeightConstraint = self.contentImageView.heightAnchor.constraint(
-            lessThanOrEqualTo: self.contentImageView.widthAnchor,
-            multiplier: height / width
-        )
+        if  height > width {
+            // show square image for portrait images
+            // restrict width to half of the screen in device landscape and to 5 / 6 in portrait
+            // it results in a good balance between message text width and image size
+            let factor: CGFloat = orientation.isLandscape ? 1 / 2 : 5 / 6
+            var squareSize  = UIScreen.main.bounds.width * factor
+
+            //reduce the image square size if there's no message text so that it fits best in the viewable area
+            if squareSize > UIScreen.main.bounds.height * 5 / 8 && (messageLabel.text?.isEmpty ?? true) {
+                squareSize = UIScreen.main.bounds.height * 5 / 8
+            }
+            imageHeightConstraint = self.contentImageView.heightAnchor.constraint(lessThanOrEqualToConstant: squareSize)
+            imageWidthConstraint = self.contentImageView.widthAnchor.constraint(lessThanOrEqualToConstant: squareSize)
+        } else {
+            // show image in aspect ratio for landscape images
+            if orientation.isLandscape && height > UIScreen.main.bounds.height * 5 / 8 {
+                //shrink landscape image in landscape device orientation if image height is too big
+                self.imageHeightConstraint = self.contentImageView.heightAnchor.constraint(lessThanOrEqualToConstant: UIScreen.main.bounds.height * 5 / 8)
+                self.imageWidthConstraint = self.contentImageView.widthAnchor.constraint(lessThanOrEqualTo: self.contentImageView.heightAnchor,
+                                                                                         multiplier: width/height)
+            } else {
+                self.imageWidthConstraint = self.contentImageView.widthAnchor.constraint(lessThanOrEqualToConstant: width)
+                self.imageHeightConstraint = self.contentImageView.heightAnchor.constraint(
+                    lessThanOrEqualTo: self.contentImageView.widthAnchor,
+                    multiplier: height / width
+                )
+            }
+        }
         self.imageHeightConstraint?.isActive = true
         self.imageWidthConstraint?.isActive = true
     }
@@ -140,6 +165,7 @@ class ImageTextCell: BaseMessageCell {
 
     override func prepareForReuse() {
         contentImageView.image = nil
+        contentImageView.sd_cancelCurrentImageLoad()
         tag = -1
     }
 }