[iOS swift] json 이미지 collectionview로 가져오기
https://github.com/SeungYeonMichelleYoo/networkBasic
ImageSearchVC
핵심은 var fetchedList: [String] = [ ] 을 선언해주어야 했던 것.
let list = json["items"].arrayValue
for object in list {
let link = object["link"].stringValue
self.fetchedList.append(link)
}
DispatchQueue.main.async {
self.mainCollectionView.reloadData()
}
위에서 var fetchedList: [String] = [ ] 선언한 것을 이용.
아이템 개수 : fetchedList.count로 가져온 것
그리고 이미지를 Xcode내에서가 아닌 웹상에서 가져올 때 쓰는 코드 참조.
https://archijude.tistory.com/183
//url에 정확한 이미지 url 주소를 넣는다.
let url = URL(string: image.url)
var image : UIImage?
//DispatchQueue를 쓰는 이유 -> 이미지가 클 경우 이미지를 다운로드 받기 까지 잠깐의 멈춤이 생길수 있다. (이유 : 싱글 쓰레드로 작동되기때문에)
//DispatchQueue를 쓰면 멀티 쓰레드로 이미지가 클경우에도 멈춤이 생기지 않는다.
DispatchQueue.global().async {
let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
DispatchQueue.main.async {
image = UIImage(data: data!)
}
}
출처: https://archijude.tistory.com/183 [글을 잠깐 혼자 써봤던 진성 프로그래머:티스토리]
'iOS > iOS SeSAC 2기 TIL' 카테고리의 다른 글
[iOS swift] xib파일은 왜 쓰는 것인가? (0) | 2022.08.04 |
---|---|
TMDB Project 과제 8.4 수업 중 피드백 (0) | 2022.08.04 |
iOS swift 타입 어노테이션 vs. 타입추론 (0) | 2022.08.03 |
iOS swift 인증키 -> git ignore (0) | 2022.08.03 |
iOS swift 어제 날짜 가져오기 (0) | 2022.08.02 |