Skip to content

Commit

Permalink
fix/#352 온보딩 이미지 캐싱문제 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
hooni0918 committed Aug 31, 2024
1 parent 6389618 commit 4a3739e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Release"
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
Expand All @@ -49,6 +49,13 @@
ReferencedContainer = "container:KkuMulKum.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<EnvironmentVariables>
<EnvironmentVariable
key = "IDEPreferLogStreaming"
value = "YES"
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import UIKit

import Kingfisher

class ProfileSetupViewModel {
Expand All @@ -17,52 +18,73 @@ class ProfileSetupViewModel {
private let authService: AuthServiceProtocol
private var imageData: Data?
private let maxImageSizeBytes = 4 * 1024 * 1024

init(nickname: String, authService: AuthServiceProtocol = AuthService()) {
self.nickname = nickname
self.authService = authService
}

func updateProfileImage(_ image: UIImage?) {
profileImage.value = image
if let image = image, let data = image.jpegData(compressionQuality: 1.0) {
imageData = data
print("이미지 크기: \(data.count) bytes")
if let image = image {
imageData = compressImage(image)
print("압축된 이미지 크기: \(imageData?.count ?? 0) bytes")
} else {
imageData = nil
}
isConfirmButtonEnabled.value = imageData != nil
clearImageCache()
}

private func compressImage(_ image: UIImage) -> Data? {
var compression: CGFloat = 1.0
var imageData = image.jpegData(compressionQuality: compression)

while (imageData?.count ?? 0) > maxImageSizeBytes && compression > 0.1 {
compression -= 0.1
imageData = image.jpegData(compressionQuality: compression)
}

return imageData
}

func uploadProfileImage() async -> Bool {
print("uploadProfileImage 함수 호출됨")
guard let imageData = imageData else {
print("이미지 데이터가 없습니다.")
serverResponse.value = "이미지 데이터가 없습니다."
return false
}

print("업로드할 이미지 데이터 크기: \(imageData.count) bytes")

let fileName = "profile_image.jpg"
let mimeType = "image/jpeg"

do {
let _: EmptyModel = try await authService.performRequest(
.updateProfileImage(
image: imageData,
fileName: fileName,
mimeType: mimeType
)
)
serverResponse.value = "프로필 이미지가 성공적으로 업로드되었습니다."
print("프로필 이미지 업로드 성공")
return true
} catch {
handleError(error as? NetworkError ?? .unknownError("알 수 없는 오류가 발생했습니다."))
return false
}
}
print("uploadProfileImage 함수 호출됨")
guard let imageData = imageData else {
print("이미지 데이터가 없습니다.")
serverResponse.value = "이미지 데이터가 없습니다."
return false
}

if imageData.count > maxImageSizeBytes {
print("이미지 크기가 최대 허용 크기를 초과합니다.")
serverResponse.value = "이미지 크기가 너무 큽니다. 더 작은 이미지를 선택해주세요."
return false
}

print("업로드할 이미지 데이터 크기: \(imageData.count) bytes")

let fileName = "profile_image.jpg"
let mimeType = "image/jpeg"

do {
let _: EmptyModel = try await authService.performRequest(
.updateProfileImage(
image: imageData,
fileName: fileName,
mimeType: mimeType
)
)
serverResponse.value = "프로필 이미지가 성공적으로 업로드되었습니다."
print("프로필 이미지 업로드 성공")

clearImageCache()
return true
} catch {
handleError(error as? NetworkError ?? .unknownError("알 수 없는 오류가 발생했습니다."))
return false
}
}

private func handleError(_ error: NetworkError) {
switch error {
Expand All @@ -81,4 +103,9 @@ class ProfileSetupViewModel {
}
print("프로필 이미지 업로드 실패: \(error.message)")
}

private func clearImageCache() {
KingfisherManager.shared.cache.clearMemoryCache()
KingfisherManager.shared.cache.clearDiskCache()
}
}

0 comments on commit 4a3739e

Please sign in to comment.