From c9a13f555cb11d0fd50dfeda556f912dae7d2327 Mon Sep 17 00:00:00 2001 From: trumanfromkorea Date: Sat, 10 Dec 2022 20:45:28 +0900 Subject: [PATCH 1/6] =?UTF-8?q?[refactor]=20NSCache+subscript=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5,=20=EA=B0=80=EC=A0=B8=EC=98=A4=EB=8A=94=20=EB=B0=A9?= =?UTF-8?q?=EC=8B=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SpaceCapsule.xcodeproj/project.pbxproj | 4 +++ .../Extensions/KingReceiver+UIImageView.swift | 1 + .../KingReceiver/Extensions/NSCache+.swift | 26 +++++++++++++++++++ .../ImageCache/MemoryImageCache.swift | 10 +++---- 4 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/NSCache+.swift diff --git a/SpaceCapsule/SpaceCapsule.xcodeproj/project.pbxproj b/SpaceCapsule/SpaceCapsule.xcodeproj/project.pbxproj index af39744..899f3aa 100644 --- a/SpaceCapsule/SpaceCapsule.xcodeproj/project.pbxproj +++ b/SpaceCapsule/SpaceCapsule.xcodeproj/project.pbxproj @@ -24,6 +24,7 @@ 0D706137292C630D0077787E /* DatePickerViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D706136292C630D0077787E /* DatePickerViewModel.swift */; }; 0D706139292C63140077787E /* DatePickerCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D706138292C63140077787E /* DatePickerCoordinator.swift */; }; 0D70613E292CD24D0077787E /* FirebaseStorageManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D70613D292CD24D0077787E /* FirebaseStorageManager.swift */; }; + 0D752DB12944A643004A704C /* NSCache+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D752DB02944A643004A704C /* NSCache+.swift */; }; 0D77D7C9292B5D490038D054 /* SelectButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D77D7C8292B5D490038D054 /* SelectButton.swift */; }; 0D90DAEF29238B77000DCC84 /* CapsuleLocateView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D90DAEE29238B77000DCC84 /* CapsuleLocateView.swift */; }; 0D90DAF129238B85000DCC84 /* CapsuleLocateCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D90DAF029238B85000DCC84 /* CapsuleLocateCoordinator.swift */; }; @@ -203,6 +204,7 @@ 0D706136292C630D0077787E /* DatePickerViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatePickerViewModel.swift; sourceTree = ""; }; 0D706138292C63140077787E /* DatePickerCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatePickerCoordinator.swift; sourceTree = ""; }; 0D70613D292CD24D0077787E /* FirebaseStorageManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirebaseStorageManager.swift; sourceTree = ""; }; + 0D752DB02944A643004A704C /* NSCache+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSCache+.swift"; sourceTree = ""; }; 0D77D7C8292B5D490038D054 /* SelectButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectButton.swift; sourceTree = ""; }; 0D90DAEE29238B77000DCC84 /* CapsuleLocateView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CapsuleLocateView.swift; sourceTree = ""; }; 0D90DAF029238B85000DCC84 /* CapsuleLocateCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CapsuleLocateCoordinator.swift; sourceTree = ""; }; @@ -907,6 +909,7 @@ isa = PBXGroup; children = ( 599C1A42292CC99900517E93 /* KingReceiver+UIImageView.swift */, + 0D752DB02944A643004A704C /* NSCache+.swift */, ); path = Extensions; sourceTree = ""; @@ -1129,6 +1132,7 @@ 599C1A4E292CC99A00517E93 /* KingReceiver+UIImageView.swift in Sources */, 288A8EA929235AFC000229D2 /* NicknameCoordinator.swift in Sources */, 288A8EE629235FD5000229D2 /* CapsuleCloseView.swift in Sources */, + 0D752DB12944A643004A704C /* NSCache+.swift in Sources */, 0D90DAF7292397CF000DCC84 /* UIColor+.swift in Sources */, 599C1A4B292CC99A00517E93 /* NoneImageCache.swift in Sources */, 28049CE729237B2B002389BD /* BaseViewController.swift in Sources */, diff --git a/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/KingReceiver+UIImageView.swift b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/KingReceiver+UIImageView.swift index 689bc12..e682ae7 100644 --- a/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/KingReceiver+UIImageView.swift +++ b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/KingReceiver+UIImageView.swift @@ -62,6 +62,7 @@ extension KingReceiverWrapper where Base: UIImageView { } stop(indicator: indicator) + indicator.translatesAutoresizingMaskIntoConstraints = false base.addSubview(indicator) diff --git a/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/NSCache+.swift b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/NSCache+.swift new file mode 100644 index 0000000..df9a7ee --- /dev/null +++ b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/NSCache+.swift @@ -0,0 +1,26 @@ +// +// NSCache+.swift +// SpaceCapsule +// +// Created by 장재훈 on 2022/12/10. +// + +import Foundation + +extension NSCache where KeyType == NSString, ObjectType == NSData { + subscript(_ url: URL) -> NSData? { + get { + let key = url.absoluteString as NSString + + return object(forKey: key) + } + + set { + let key = url.absoluteString as NSString + + if let newValue { + setObject(newValue, forKey: key) + } + } + } +} diff --git a/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/ImageCache/MemoryImageCache.swift b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/ImageCache/MemoryImageCache.swift index bbd0b41..844400e 100644 --- a/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/ImageCache/MemoryImageCache.swift +++ b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/ImageCache/MemoryImageCache.swift @@ -12,9 +12,7 @@ final class MemoryImageCache: ImageCache { private let cache = NSCache() func fetch(with url: URL, completion: @escaping (Data?) -> Void) { - let key = url.absoluteString as NSString - - if let data = cache.object(forKey: key) { + if let data = cache[url] { completion(data as Data) return } @@ -25,12 +23,12 @@ final class MemoryImageCache: ImageCache { return } - self?.save(data: data, with: key) + self?.save(data: data, with: url) completion(data) } } - func save(data: Data, with key: NSString) { - cache.setObject(data as NSData, forKey: key) + func save(data: Data, with url: URL) { + cache[url] = data as NSData } } From b1b78f96181daddf827dfc98369a4adf9c1ea1ee Mon Sep 17 00:00:00 2001 From: trumanfromkorea Date: Sat, 10 Dec 2022 20:49:08 +0900 Subject: [PATCH 2/6] =?UTF-8?q?[refactor]=20UIImage+resize=20=EC=9C=84?= =?UTF-8?q?=EC=B9=98=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SpaceCapsule.xcodeproj/project.pbxproj | 12 +++-- .../SpaceCapsule/Extensions/UIImage+.swift | 35 --------------- ...NSCache+.swift => NSCache+subscript.swift} | 0 .../Extensions/UIImage+resize.swift | 44 +++++++++++++++++++ 4 files changed, 52 insertions(+), 39 deletions(-) rename SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/{NSCache+.swift => NSCache+subscript.swift} (100%) create mode 100644 SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/UIImage+resize.swift diff --git a/SpaceCapsule/SpaceCapsule.xcodeproj/project.pbxproj b/SpaceCapsule/SpaceCapsule.xcodeproj/project.pbxproj index 899f3aa..689cc86 100644 --- a/SpaceCapsule/SpaceCapsule.xcodeproj/project.pbxproj +++ b/SpaceCapsule/SpaceCapsule.xcodeproj/project.pbxproj @@ -24,7 +24,8 @@ 0D706137292C630D0077787E /* DatePickerViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D706136292C630D0077787E /* DatePickerViewModel.swift */; }; 0D706139292C63140077787E /* DatePickerCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D706138292C63140077787E /* DatePickerCoordinator.swift */; }; 0D70613E292CD24D0077787E /* FirebaseStorageManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D70613D292CD24D0077787E /* FirebaseStorageManager.swift */; }; - 0D752DB12944A643004A704C /* NSCache+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D752DB02944A643004A704C /* NSCache+.swift */; }; + 0D752DB12944A643004A704C /* NSCache+subscript.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D752DB02944A643004A704C /* NSCache+subscript.swift */; }; + 0D752DB32944A964004A704C /* UIImage+resize.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D752DB22944A964004A704C /* UIImage+resize.swift */; }; 0D77D7C9292B5D490038D054 /* SelectButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D77D7C8292B5D490038D054 /* SelectButton.swift */; }; 0D90DAEF29238B77000DCC84 /* CapsuleLocateView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D90DAEE29238B77000DCC84 /* CapsuleLocateView.swift */; }; 0D90DAF129238B85000DCC84 /* CapsuleLocateCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D90DAF029238B85000DCC84 /* CapsuleLocateCoordinator.swift */; }; @@ -204,7 +205,8 @@ 0D706136292C630D0077787E /* DatePickerViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatePickerViewModel.swift; sourceTree = ""; }; 0D706138292C63140077787E /* DatePickerCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatePickerCoordinator.swift; sourceTree = ""; }; 0D70613D292CD24D0077787E /* FirebaseStorageManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirebaseStorageManager.swift; sourceTree = ""; }; - 0D752DB02944A643004A704C /* NSCache+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSCache+.swift"; sourceTree = ""; }; + 0D752DB02944A643004A704C /* NSCache+subscript.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSCache+subscript.swift"; sourceTree = ""; }; + 0D752DB22944A964004A704C /* UIImage+resize.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+resize.swift"; sourceTree = ""; }; 0D77D7C8292B5D490038D054 /* SelectButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectButton.swift; sourceTree = ""; }; 0D90DAEE29238B77000DCC84 /* CapsuleLocateView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CapsuleLocateView.swift; sourceTree = ""; }; 0D90DAF029238B85000DCC84 /* CapsuleLocateCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CapsuleLocateCoordinator.swift; sourceTree = ""; }; @@ -909,7 +911,8 @@ isa = PBXGroup; children = ( 599C1A42292CC99900517E93 /* KingReceiver+UIImageView.swift */, - 0D752DB02944A643004A704C /* NSCache+.swift */, + 0D752DB02944A643004A704C /* NSCache+subscript.swift */, + 0D752DB22944A964004A704C /* UIImage+resize.swift */, ); path = Extensions; sourceTree = ""; @@ -1132,7 +1135,7 @@ 599C1A4E292CC99A00517E93 /* KingReceiver+UIImageView.swift in Sources */, 288A8EA929235AFC000229D2 /* NicknameCoordinator.swift in Sources */, 288A8EE629235FD5000229D2 /* CapsuleCloseView.swift in Sources */, - 0D752DB12944A643004A704C /* NSCache+.swift in Sources */, + 0D752DB12944A643004A704C /* NSCache+subscript.swift in Sources */, 0D90DAF7292397CF000DCC84 /* UIColor+.swift in Sources */, 599C1A4B292CC99A00517E93 /* NoneImageCache.swift in Sources */, 28049CE729237B2B002389BD /* BaseViewController.swift in Sources */, @@ -1195,6 +1198,7 @@ 28A700D52938AE100049FAF8 /* AnimationResource.swift in Sources */, 288A8EE229235E3F000229D2 /* CapsuleLocateViewModel.swift in Sources */, 59D8CFBC29360A8900FD9CF6 /* ThemeThumbnailImageView.swift in Sources */, + 0D752DB32944A964004A704C /* UIImage+resize.swift in Sources */, 0D4C11372927489D00B1C2B6 /* UIViewPreview.swift in Sources */, 288A8EB229235B5A000229D2 /* ProfileView.swift in Sources */, 599C1A48292CC99A00517E93 /* KingReceiver.swift in Sources */, diff --git a/SpaceCapsule/SpaceCapsule/Extensions/UIImage+.swift b/SpaceCapsule/SpaceCapsule/Extensions/UIImage+.swift index 2ed2570..135bb4f 100644 --- a/SpaceCapsule/SpaceCapsule/Extensions/UIImage+.swift +++ b/SpaceCapsule/SpaceCapsule/Extensions/UIImage+.swift @@ -36,38 +36,3 @@ extension UIImage { static let refresh = UIImage(systemName: "arrow.clockwise") } -extension UIImage { - func resize(_ newWidth: CGFloat) -> UIImage { - let scale = newWidth / self.size.width - let newHeight = self.size.height * scale - - let size = CGSize(width: newWidth, height: newHeight) - let render = UIGraphicsImageRenderer(size: size) - let renderImage = render.image { _ in - self.draw(in: CGRect(origin: .zero, size: size)) - } - - return renderImage - } - - static func resize(data: Data, to targetSize: CGSize, scale: CGFloat) -> UIImage? { - let imageSourceOptions = [kCGImageSourceShouldCache: false] as CFDictionary - guard let imageSource = CGImageSourceCreateWithData(data as CFData, imageSourceOptions) else { - return nil - } - - let maxDimension = max(targetSize.width, targetSize.height) * scale - let resizingOptions = [ - kCGImageSourceCreateThumbnailFromImageAlways: true, - kCGImageSourceShouldCacheImmediately: true, - kCGImageSourceCreateThumbnailWithTransform: true, - kCGImageSourceThumbnailMaxPixelSize: maxDimension, - ] as CFDictionary - - guard let resizedImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, resizingOptions) else { - return nil - } - - return UIImage(cgImage: resizedImage) - } -} diff --git a/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/NSCache+.swift b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/NSCache+subscript.swift similarity index 100% rename from SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/NSCache+.swift rename to SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/NSCache+subscript.swift diff --git a/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/UIImage+resize.swift b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/UIImage+resize.swift new file mode 100644 index 0000000..bad0d28 --- /dev/null +++ b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/UIImage+resize.swift @@ -0,0 +1,44 @@ +// +// UIImage+.swift +// SpaceCapsule +// +// Created by 장재훈 on 2022/12/10. +// + +import UIKit.UIImage + +extension UIImage { + func resize(_ newWidth: CGFloat) -> UIImage { + let scale = newWidth / self.size.width + let newHeight = self.size.height * scale + + let size = CGSize(width: newWidth, height: newHeight) + let render = UIGraphicsImageRenderer(size: size) + let renderImage = render.image { _ in + self.draw(in: CGRect(origin: .zero, size: size)) + } + + return renderImage + } + + static func resize(data: Data, to targetSize: CGSize, scale: CGFloat) -> UIImage? { + let imageSourceOptions = [kCGImageSourceShouldCache: false] as CFDictionary + guard let imageSource = CGImageSourceCreateWithData(data as CFData, imageSourceOptions) else { + return nil + } + + let maxDimension = max(targetSize.width, targetSize.height) * scale + let resizingOptions = [ + kCGImageSourceCreateThumbnailFromImageAlways: true, + kCGImageSourceShouldCacheImmediately: true, + kCGImageSourceCreateThumbnailWithTransform: true, + kCGImageSourceThumbnailMaxPixelSize: maxDimension, + ] as CFDictionary + + guard let resizedImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, resizingOptions) else { + return nil + } + + return UIImage(cgImage: resizedImage) + } +} From 134eb70e081c34ef782aa1baa8eff2f0c867577f Mon Sep 17 00:00:00 2001 From: trumanfromkorea Date: Sat, 10 Dec 2022 22:46:06 +0900 Subject: [PATCH 3/6] =?UTF-8?q?[refactor]=20NSCache=20=EC=9A=A9=EB=9F=89?= =?UTF-8?q?=20=EC=A1=B0=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Manager/KingReceiver/ImageCache/MemoryImageCache.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/ImageCache/MemoryImageCache.swift b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/ImageCache/MemoryImageCache.swift index 844400e..9468aed 100644 --- a/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/ImageCache/MemoryImageCache.swift +++ b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/ImageCache/MemoryImageCache.swift @@ -9,7 +9,12 @@ import Foundation /// 메모리 -> `NSCache` 에 캐싱 final class MemoryImageCache: ImageCache { - private let cache = NSCache() + private let cache: NSCache = { + let cache = NSCache() + cache.totalCostLimit = 52428800 + + return cache + }() func fetch(with url: URL, completion: @escaping (Data?) -> Void) { if let data = cache[url] { From 2b3e288a6011191c6e0f69793006d5bdb8ee3270 Mon Sep 17 00:00:00 2001 From: trumanfromkorea Date: Sat, 10 Dec 2022 23:18:40 +0900 Subject: [PATCH 4/6] =?UTF-8?q?[refactor]=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20?= =?UTF-8?q?=EB=8B=A4=EC=9A=B4=EC=83=98=ED=94=8C=EB=A7=81=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EB=B3=80=EA=B2=BD=EC=A4=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/KingReceiver+UIImageView.swift | 4 +- .../Extensions/UIImage+resize.swift | 44 +++++++++++++++---- .../Components/AddImageCell.swift | 2 +- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/KingReceiver+UIImageView.swift b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/KingReceiver+UIImageView.swift index e682ae7..6799ef1 100644 --- a/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/KingReceiver+UIImageView.swift +++ b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/KingReceiver+UIImageView.swift @@ -46,13 +46,13 @@ extension KingReceiverWrapper where Base: UIImageView { } } - func setImage(with data: Data, placeholder: UIImage? = nil, size: CGFloat) { + func setImage(with data: Data, placeholder: UIImage? = nil, width: CGFloat, scale: CGFloat = 1) { guard let image = UIImage(data: data) else { base.image = placeholder return } - base.image = image.resize(size) + base.image = image.resize(newWidth: width, scale: scale) } /// 로딩 indicator 시작 diff --git a/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/UIImage+resize.swift b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/UIImage+resize.swift index bad0d28..f1df272 100644 --- a/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/UIImage+resize.swift +++ b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/UIImage+resize.swift @@ -8,17 +8,43 @@ import UIKit.UIImage extension UIImage { - func resize(_ newWidth: CGFloat) -> UIImage { - let scale = newWidth / self.size.width - let newHeight = self.size.height * scale - - let size = CGSize(width: newWidth, height: newHeight) - let render = UIGraphicsImageRenderer(size: size) - let renderImage = render.image { _ in - self.draw(in: CGRect(origin: .zero, size: size)) +// func resize(_ newWidth: CGFloat) -> UIImage { +// let scale = newWidth / self.size.width +// let newHeight = self.size.height * scale +// +// let size = CGSize(width: newWidth, height: newHeight) +// let render = UIGraphicsImageRenderer(size: size) +// let renderImage = render.image { _ in +// self.draw(in: CGRect(origin: .zero, size: size)) +// } +// +// return renderImage +// } + + func resize(newWidth: CGFloat, scale: CGFloat) -> UIImage? { + let imageSourceOptions = [kCGImageSourceShouldCache: false] as CFDictionary + + guard let data = pngData(), + let imageSource = CGImageSourceCreateWithData(data as CFData, imageSourceOptions) else { + return nil } - return renderImage + let ratio = newWidth / self.size.width + let newHeight = self.size.height * ratio + let maxDimension = max(newWidth, newHeight) * scale + + let resizingOptions = [ + kCGImageSourceCreateThumbnailFromImageAlways: true, + kCGImageSourceShouldCacheImmediately: true, + kCGImageSourceCreateThumbnailWithTransform: true, + kCGImageSourceThumbnailMaxPixelSize: maxDimension, + ] as CFDictionary + + guard let resizedImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, resizingOptions) else { + return nil + } + + return UIImage(cgImage: resizedImage) } static func resize(data: Data, to targetSize: CGSize, scale: CGFloat) -> UIImage? { diff --git a/SpaceCapsule/SpaceCapsule/Scene/TabBar/CapsuleAdd/CapsuleCreate/Components/AddImageCell.swift b/SpaceCapsule/SpaceCapsule/Scene/TabBar/CapsuleAdd/CapsuleCreate/Components/AddImageCell.swift index 5f4fa4e..1cb6721 100644 --- a/SpaceCapsule/SpaceCapsule/Scene/TabBar/CapsuleAdd/CapsuleCreate/Components/AddImageCell.swift +++ b/SpaceCapsule/SpaceCapsule/Scene/TabBar/CapsuleAdd/CapsuleCreate/Components/AddImageCell.swift @@ -31,7 +31,7 @@ final class AddImageCell: UICollectionViewCell { } func configure(data: Data) { - imageView.kr.setImage(with: data, placeholder: .empty, size: frame.size.width) + imageView.kr.setImage(with: data, placeholder: .empty, width: frame.size.width) } private func addSubViews() { From 382772bc79fad20adefdae49fac03a455e778c6c Mon Sep 17 00:00:00 2001 From: trumanfromkorea Date: Mon, 12 Dec 2022 13:54:26 +0900 Subject: [PATCH 5/6] =?UTF-8?q?[fix]=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20?= =?UTF-8?q?=EB=8B=A4=EC=9A=B4=EC=83=98=ED=94=8C=EB=A7=81=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EB=B3=80=EA=B2=BD=20(#97)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Map/CustomAnnotationView.swift | 12 +++++- .../Extensions/KingReceiver+UIImageView.swift | 4 +- .../Extensions/UIImage+resize.swift | 39 ------------------- 3 files changed, 13 insertions(+), 42 deletions(-) diff --git a/SpaceCapsule/SpaceCapsule/Components/Map/CustomAnnotationView.swift b/SpaceCapsule/SpaceCapsule/Components/Map/CustomAnnotationView.swift index ff793b7..15041a9 100644 --- a/SpaceCapsule/SpaceCapsule/Components/Map/CustomAnnotationView.swift +++ b/SpaceCapsule/SpaceCapsule/Components/Map/CustomAnnotationView.swift @@ -40,6 +40,16 @@ final class CustomAnnotationView: MKAnnotationView { } func update(for annotation: MKAnnotation?) { - image = (annotation as? CustomAnnotation)?.pinImage?.resize(40) + guard let pinImage = (annotation as? CustomAnnotation)?.pinImage, + let imageData = pinImage.pngData() else { + return + } + + let newWidth: CGFloat = 40 + let ratio = newWidth / pinImage.size.width + let newHeight = pinImage.size.height * ratio + let size = CGSize(width: newWidth, height: newHeight) + + image = UIImage.resize(data: imageData, to: size, scale: 1) } } diff --git a/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/KingReceiver+UIImageView.swift b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/KingReceiver+UIImageView.swift index 6799ef1..0f26f62 100644 --- a/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/KingReceiver+UIImageView.swift +++ b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/KingReceiver+UIImageView.swift @@ -47,12 +47,12 @@ extension KingReceiverWrapper where Base: UIImageView { } func setImage(with data: Data, placeholder: UIImage? = nil, width: CGFloat, scale: CGFloat = 1) { - guard let image = UIImage(data: data) else { + guard let image = UIImage.resize(data: data, to: base.frame.size, scale: scale) else { base.image = placeholder return } - base.image = image.resize(newWidth: width, scale: scale) + base.image = image } /// 로딩 indicator 시작 diff --git a/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/UIImage+resize.swift b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/UIImage+resize.swift index f1df272..725ad95 100644 --- a/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/UIImage+resize.swift +++ b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/Extensions/UIImage+resize.swift @@ -8,45 +8,6 @@ import UIKit.UIImage extension UIImage { -// func resize(_ newWidth: CGFloat) -> UIImage { -// let scale = newWidth / self.size.width -// let newHeight = self.size.height * scale -// -// let size = CGSize(width: newWidth, height: newHeight) -// let render = UIGraphicsImageRenderer(size: size) -// let renderImage = render.image { _ in -// self.draw(in: CGRect(origin: .zero, size: size)) -// } -// -// return renderImage -// } - - func resize(newWidth: CGFloat, scale: CGFloat) -> UIImage? { - let imageSourceOptions = [kCGImageSourceShouldCache: false] as CFDictionary - - guard let data = pngData(), - let imageSource = CGImageSourceCreateWithData(data as CFData, imageSourceOptions) else { - return nil - } - - let ratio = newWidth / self.size.width - let newHeight = self.size.height * ratio - let maxDimension = max(newWidth, newHeight) * scale - - let resizingOptions = [ - kCGImageSourceCreateThumbnailFromImageAlways: true, - kCGImageSourceShouldCacheImmediately: true, - kCGImageSourceCreateThumbnailWithTransform: true, - kCGImageSourceThumbnailMaxPixelSize: maxDimension, - ] as CFDictionary - - guard let resizedImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, resizingOptions) else { - return nil - } - - return UIImage(cgImage: resizedImage) - } - static func resize(data: Data, to targetSize: CGSize, scale: CGFloat) -> UIImage? { let imageSourceOptions = [kCGImageSourceShouldCache: false] as CFDictionary guard let imageSource = CGImageSourceCreateWithData(data as CFData, imageSourceOptions) else { From 04b3960d4651daee9509c57c1c2e39b81396a535 Mon Sep 17 00:00:00 2001 From: trumanfromkorea Date: Mon, 12 Dec 2022 14:16:17 +0900 Subject: [PATCH 6/6] =?UTF-8?q?[fix]=20=EB=A9=94=EB=AA=A8=EB=A6=AC=20?= =?UTF-8?q?=EC=BA=90=EC=8B=9C=20=EC=9A=A9=EB=9F=89=20=EC=A7=80=EC=A0=95?= =?UTF-8?q?=EB=B0=A9=EC=8B=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../KingReceiver/ImageCache/MemoryImageCache.swift | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/ImageCache/MemoryImageCache.swift b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/ImageCache/MemoryImageCache.swift index 9468aed..52aaa1f 100644 --- a/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/ImageCache/MemoryImageCache.swift +++ b/SpaceCapsule/SpaceCapsule/Manager/KingReceiver/ImageCache/MemoryImageCache.swift @@ -9,12 +9,11 @@ import Foundation /// 메모리 -> `NSCache` 에 캐싱 final class MemoryImageCache: ImageCache { - private let cache: NSCache = { - let cache = NSCache() - cache.totalCostLimit = 52428800 + private let cache = NSCache() - return cache - }() + init(cacheLimit: Int = 52428800) { + cache.totalCostLimit = cacheLimit + } func fetch(with url: URL, completion: @escaping (Data?) -> Void) { if let data = cache[url] {