Skip to content

Commit c324ced

Browse files
committed
Added optional aspect ratio constraint.
1 parent e6ea756 commit c324ced

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

Sources/SMLinkPreview/LinkPreview.swift

+32-4
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,16 @@ public class LinkPreview: UIView {
121121
if let sizingNumberTitleLines = sizing?.titleLabelNumberOfLines {
122122
title.numberOfLines = sizingNumberTitleLines
123123
}
124-
124+
125125
if let imageURL = linkData.image,
126126
let data = try? Data(contentsOf: imageURL.attemptForceScheme(forceScheme)) {
127127
haveImage = true
128128
image.image = UIImage(data: data)
129-
if let image = image.image {
129+
130+
if let image = getImage(image: image.image) {
130131
result = .large(image)
131132
}
133+
132134
applyCornerRounding(view: contentView)
133135

134136
// Not showing the icon-- because we have the large image
@@ -153,8 +155,9 @@ public class LinkPreview: UIView {
153155
if let iconURL = linkData.icon {
154156
if let data = try? Data(contentsOf: iconURL.attemptForceScheme(forceScheme)) {
155157
icon.image = UIImage(data: data)
156-
if let icon = icon.image {
157-
result = .icon(icon)
158+
159+
if let image = getImage(image: image.image) {
160+
result = .icon(image)
158161
}
159162
}
160163
}
@@ -172,6 +175,19 @@ public class LinkPreview: UIView {
172175

173176
callback?(result)
174177
}
178+
179+
func getImage(image: UIImage?) -> UIImage? {
180+
if let minAspectRatio = PreviewManager.session.config.minimumImageAspectRatio {
181+
if let image = image,
182+
image.size.aspectRatioOK(minimumAspectRatio: minAspectRatio) {
183+
return image
184+
}
185+
else {
186+
return nil
187+
}
188+
}
189+
return image
190+
}
175191

176192
override public func layoutSubviews() {
177193
super.layoutSubviews()
@@ -193,3 +209,15 @@ public class LinkPreview: UIView {
193209
textAndIconAction?()
194210
}
195211
}
212+
213+
extension CGSize {
214+
func aspectRatioOK(minimumAspectRatio: CGFloat = 0.05) -> Bool {
215+
if width == 0 || height == 0 {
216+
return false
217+
}
218+
219+
let maxDim = max(width, height)
220+
let minDim = min(width, height)
221+
return (minDim / maxDim) >= minimumAspectRatio
222+
}
223+
}

Sources/SMLinkPreview/PreviewManager.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
//
77

88
import Foundation
9+
import UIKit
910

1011
public struct PreviewConfiguration {
1112
public let alwaysUseHTTPS: Bool // Use https for all remote image loading? Default is true because otherwise app needs ATS set to allow insecure loading. This is not relevant for local image loading.
1213
public let maxNumberTitleLines: UInt // 0 means unlimited
14+
public let minimumImageAspectRatio: CGFloat?
1315

14-
public init(alwaysUseHTTPS: Bool = true, maxNumberTitleLines: UInt = 0) {
16+
public init(alwaysUseHTTPS: Bool = true, maxNumberTitleLines: UInt = 0, minimumImageAspectRatio: CGFloat? = nil) {
1517
self.alwaysUseHTTPS = alwaysUseHTTPS
1618
self.maxNumberTitleLines = maxNumberTitleLines
19+
self.minimumImageAspectRatio = minimumImageAspectRatio
1720
}
1821
}
1922

0 commit comments

Comments
 (0)