Skip to content

Commit

Permalink
[Feat] Keyneez#46 - 홈 이미지 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
kpk0616 committed Jan 13, 2023
1 parent 0c338e7 commit 2ba39ab
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Keyneez/Keyneez/Global/Auth/UserSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ final class UserSession {
private init() { }

var profile: Profile?
var accessToken: String? = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2tleSI6MjY3LCJpYXQiOjE2NzM2MTk4NjIsImV4cCI6MTY3MzYyNzA2Mn0.oHAGQsfqdn6f_1iucx1wSHsiNacRUgMxv1aqLyADue0"
var accessToken: String? = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2tleSI6Mjc1LCJpYXQiOjE2NzM2MjcyNjQsImV4cCI6MTY3MzYzNDQ2NH0.BQ1kUEycoo7tfP5kmiQcETR6CtS3wpiqxeaqZDSfO-E"

}
70 changes: 43 additions & 27 deletions Keyneez/Keyneez/Global/Views/UIImageView+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,52 @@
import UIKit
import Kingfisher
import FirebaseStorage
//
//extension UIImageView {
// func setImage(with urlString: String) {
// let cache = ImageCache.default
// guard let imageUrl = urlString.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) else {return}
//// let imageUrl = "\(StorageService.shared.imageBaseUrl)\(newUrlString)"
// print("캐싱한것 찾을떄 쓰이는 url \(imageUrl)")
// cache.retrieveImage(forKey: imageUrl) { (result) in
// switch result {
// case .success(let value):
// if let image = value.image {
// print("캐시가 된것을 꺼내쓴다.")
// self.image = image
// } else {
// let storage = Storage.storage()
// storage.reference(forURL: imageUrl).downloadURL { (url, error) in
// if let error = error {
// print("ERROR \(error.localizedDescription)")
// return
// }
// guard let url = url else {
// return
// }
// print("이미지 캐시가 되지 않아서 다시 다운받는다.", url)
// let resource = ImageResource(downloadURL: url, cacheKey: imageUrl)
// self.kf.setImage(with: resource)
// }
// }
// case .failure(let error):
// print("error \(error.localizedDescription)")
// }
// }
// }
//}

extension UIImageView {
func setImage(with UrlString: String) {
let cache = ImageCache.default
guard let newUrlString = UrlString.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) else {return}
let imageUrl = "\(StorageService.shared.imageBaseUrl)\(newUrlString)"
print("캐싱한것 찾을떄 쓰이는 url \(imageUrl)")
cache.retrieveImage(forKey: imageUrl) { (result) in
switch result {
case .success(let value):
if let image = value.image {
print("캐시가 된것을 꺼내쓴다.")
self.image = image
} else {
let storage = Storage.storage()
storage.reference(forURL: imageUrl).downloadURL { (url, error) in
if let error = error {
print("ERROR \(error.localizedDescription)")
return
}
guard let url = url else {
return
}
print("이미지 캐시가 되지 않아서 다시 다운받는다.", url)
let resource = ImageResource(downloadURL: url, cacheKey: imageUrl)
self.kf.setImage(with: resource)
}
func setImage(url: String) {
if let imageUrl = URL(string: url) {
let resource = ImageResource(downloadURL: imageUrl)
self.kf.setImage(with: resource) { (result) in
switch result {
case .success(_):
print("success")
case .failure(_):
print("fail")
}
case .failure(let error):
print("error \(error.localizedDescription)")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
import UIKit
import Then
import SnapKit
import Kingfisher
import FirebaseStorage

final class HomeContentCollectionViewCell: UICollectionViewCell {
static let identifier = "HomeContentCollectionViewCell"
var homeContentID = -1
var homeContentImageURL: String = ""

let repository: ContentRepository = KeyneezContentRepository()
// MARK: - UI Components
Expand Down Expand Up @@ -140,6 +143,8 @@ extension HomeContentCollectionViewCell {
contentTitle.text = setTitle(fullTitle: model.contentTitle)
contentIntroduction.text = model.introduction
likeButton.isSelected = model.liked
// contentImageView.setImage(with: model.contentImg)
contentImageView.setImage(url: model.contentImg)
}
private func setTitle(fullTitle: String) -> String {
guard let title = fullTitle as? String else {return ""}
Expand Down

0 comments on commit 2ba39ab

Please sign in to comment.