이미지 URL 주소를 Combine 을 사용하여 UIImage 로 바꿀 수 있습니다.
private func requestImageView(with urlStr: String) -> AnyPublisher<UIImage, Error> {
guard let urlDecStr = urlStr.removingPercentEncoding,
let urlEncStr = urlDecStr.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed),
let url = URL(string: urlEncStr)
else {
return Fail(error: URLError(.badURL)).eraseToAnyPublisher()
} // Optional Biding 처리 || 에러 발생 하면 알림
return URLSession.shared.dataTaskPublisher(for: url)
.tryMap { element -> UIImage in
guard let httpResponse = element.response as? HTTPURLResponse,
httpResponse.statusCode == 200
else {
throw URLError(.badServerResponse)
}
guard let image = UIImage(data: element.data) else {
throw URLError(.cannotDecodeContentData)
}
return image
}
.receive(on: RunLoop.main)
.eraseToAnyPublisher()
}