Fix image sizes.

This commit is contained in:
Marcin Czachursk 2023-03-11 18:53:58 +01:00
parent 79bc4f4539
commit 99e0429d51
3 changed files with 32 additions and 7 deletions

View File

@ -1221,7 +1221,7 @@
CODE_SIGN_ENTITLEMENTS = VernissageWidgetExtension.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 63;
CURRENT_PROJECT_VERSION = 64;
DEVELOPMENT_TEAM = B2U9FEKYP8;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = VernissageWidget/Info.plist;
@ -1249,7 +1249,7 @@
ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground;
CODE_SIGN_ENTITLEMENTS = VernissageWidgetExtension.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 63;
CURRENT_PROJECT_VERSION = 64;
DEVELOPMENT_TEAM = B2U9FEKYP8;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = VernissageWidget/Info.plist;
@ -1393,7 +1393,7 @@
CODE_SIGN_ENTITLEMENTS = Vernissage/Vernissage.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 63;
CURRENT_PROJECT_VERSION = 64;
DEVELOPMENT_ASSET_PATHS = "\"Vernissage/Preview Content\"";
DEVELOPMENT_TEAM = B2U9FEKYP8;
ENABLE_PREVIEWS = YES;
@ -1431,7 +1431,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = Vernissage/Vernissage.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 63;
CURRENT_PROJECT_VERSION = 64;
DEVELOPMENT_ASSET_PATHS = "\"Vernissage/Preview Content\"";
DEVELOPMENT_TEAM = B2U9FEKYP8;
ENABLE_PREVIEWS = YES;

View File

@ -17,4 +17,15 @@ extension UIImage {
_ in draw(in: CGRect(origin: .zero, size: canvas))
}
}
func resized(toHeight height: CGFloat, isOpaque: Bool = true) -> UIImage? {
let canvas = CGSize(width: CGFloat(ceil(height/size.height * size.width)), height: height)
let format = imageRendererFormat
format.opaque = isOpaque
return UIGraphicsImageRenderer(size: canvas, format: format).image {
_ in draw(in: CGRect(origin: .zero, size: canvas))
}
}
}

View File

@ -12,6 +12,8 @@ public class ImageFetcher {
public static let shared = ImageFetcher()
private init() { }
private let maxImageSize = 1000.0
func fetchWidgetEntries(length: Int = 6) async throws -> [WidgetEntry] {
let defaultSettings = ApplicationSettingsHandler.shared.getDefaultSettings()
guard let accountId = defaultSettings.currentAccount else {
@ -80,11 +82,23 @@ public class ImageFetcher {
do {
let (data, response) = try await URLSession.shared.data(from: url)
if (response as? HTTPURLResponse)?.status?.responseType == .success {
return UIImage(data: data)?.resized(toWidth: 1200)
guard (response as? HTTPURLResponse)?.status?.responseType == .success else {
return nil
}
return nil
guard let uiImage = UIImage(data: data) else {
return nil
}
if uiImage.size.width < self.maxImageSize && uiImage.size.height < self.maxImageSize {
return uiImage
}
if uiImage.size.width > uiImage.size.height {
return uiImage.resized(toWidth: self.maxImageSize)
} else {
return uiImage.resized(toHeight: self.maxImageSize)
}
} catch {
return nil
}