damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit cdacbcfdca3b255876c154dc87a4b51fdc51aada
parent 41e036cff23be8ae41d9e6c736d6ca2a4cd6942d
Author: Suhail Saqan <suhail.saqan@gmail.com>
Date:   Tue, 19 Sep 2023 13:24:11 -0700

util: add ImageResizer to change size of images

Diffstat:
Mdamus.xcodeproj/project.pbxproj | 12++++++++++++
Adamus/Models/Camera/ImageResizer.swift | 40++++++++++++++++++++++++++++++++++++++++
2 files changed, 52 insertions(+), 0 deletions(-)

diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj @@ -415,6 +415,7 @@ 9609F058296E220800069BF3 /* BannerImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9609F057296E220800069BF3 /* BannerImageView.swift */; }; 9C83F89329A937B900136C08 /* TextViewWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9C83F89229A937B900136C08 /* TextViewWrapper.swift */; }; 9CA876E229A00CEA0003B9A3 /* AttachMediaUtility.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9CA876E129A00CE90003B9A3 /* AttachMediaUtility.swift */; }; + BA37598A2ABCCDE40018D73B /* ImageResizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA3759892ABCCDE30018D73B /* ImageResizer.swift */; }; BA4AB0AE2A63B9270070A32A /* AddEmojiView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA4AB0AD2A63B9270070A32A /* AddEmojiView.swift */; }; BA4AB0B02A63B94D0070A32A /* EmojiListItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA4AB0AF2A63B94D0070A32A /* EmojiListItemView.swift */; }; BA693074295D649800ADDB87 /* UserSettingsStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA693073295D649800ADDB87 /* UserSettingsStore.swift */; }; @@ -1094,6 +1095,7 @@ 9609F057296E220800069BF3 /* BannerImageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BannerImageView.swift; sourceTree = "<group>"; }; 9C83F89229A937B900136C08 /* TextViewWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextViewWrapper.swift; sourceTree = "<group>"; }; 9CA876E129A00CE90003B9A3 /* AttachMediaUtility.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttachMediaUtility.swift; sourceTree = "<group>"; }; + BA3759892ABCCDE30018D73B /* ImageResizer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageResizer.swift; sourceTree = "<group>"; }; BA4AB0AD2A63B9270070A32A /* AddEmojiView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddEmojiView.swift; sourceTree = "<group>"; }; BA4AB0AF2A63B94D0070A32A /* EmojiListItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmojiListItemView.swift; sourceTree = "<group>"; }; BA693073295D649800ADDB87 /* UserSettingsStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserSettingsStore.swift; sourceTree = "<group>"; }; @@ -1239,6 +1241,7 @@ 4C0A3F8D280F63FF000448DE /* Models */ = { isa = PBXGroup; children = ( + BA3759882ABCCDE30018D73B /* Camera */, 4C190F1E2A535FC200027FD5 /* Zaps */, 4C54AA0829A55416003E4487 /* Notifications */, 3AA247FC297E3CFF0090C62D /* RepostsModel.swift */, @@ -2251,6 +2254,14 @@ path = Extensions; sourceTree = "<group>"; }; + BA3759882ABCCDE30018D73B /* Camera */ = { + isa = PBXGroup; + children = ( + BA3759892ABCCDE30018D73B /* ImageResizer.swift */, + ); + path = Camera; + sourceTree = "<group>"; + }; F71694E82A66221E001F4053 /* Onboarding */ = { isa = PBXGroup; children = ( @@ -2794,6 +2805,7 @@ 4C3BEFDA281DCA1400B3DE84 /* LikeCounter.swift in Sources */, 4C32B9502A9AD44700DC3548 /* FlatBufferBuilder.swift in Sources */, 50A60D142A28BEEE00186190 /* RelayLog.swift in Sources */, + BA37598A2ABCCDE40018D73B /* ImageResizer.swift in Sources */, 4CB88389296AF99A00DC99E7 /* EventDetailBar.swift in Sources */, 4C32B9512A9AD44700DC3548 /* FlatbuffersErrors.swift in Sources */, 4CE8794E2996B16A00F758CC /* RelayToggle.swift in Sources */, diff --git a/damus/Models/Camera/ImageResizer.swift b/damus/Models/Camera/ImageResizer.swift @@ -0,0 +1,40 @@ +// +// ImageResizer.swift +// damus +// +// Created by Suhail Saqan on 8/5/23. +// + +import Foundation +import UIKit + +public enum ImageResizingError: Error { + case cannotRetrieveFromURL + case cannotRetrieveFromData +} + +public struct ImageResizer { + public var targetWidth: CGFloat + + public init(targetWidth: CGFloat) { + self.targetWidth = targetWidth + } + + public func resize(at url: URL) -> UIImage? { + guard let image = UIImage(contentsOfFile: url.path) else { + return nil + } + + return self.resize(image: image) + } + + public func resize(image: UIImage) -> UIImage { + let originalSize = image.size + let targetSize = CGSize(width: targetWidth, height: targetWidth*originalSize.height/originalSize.width) + let renderer = UIGraphicsImageRenderer(size: targetSize) + return renderer.image { (context) in + image.draw(in: CGRect(origin: .zero, size: targetSize)) + } + } +} +