77: Milestone of Projects 13-15

그루두·2024년 7월 27일
0

100 days of SwiftUI

목록 보기
85/108

Milestone: Projects 13-15

challenge

  • Use PhotosPicker to let users import a photo from their photo library.
  • Detect when a new photo is imported, and immediately ask the user to name the photo.
  • Save that name and photo somewhere safe.
  • Show all names and photos in a list, sorted by name.
  • Create a detail screen that shows a picture full size.
  • Decide on a way to save all this data.

solution

  1. Photo model 만들기
  2. AddPhotoView 만들어서 사진 받아와서 Data로 변환하고 modelContext에 저장하기
  3. name 작성 안 하거나 사진이 없으면 Save 버튼 .disable 설정하기
  4. photos 정렬하기
  5. Data로 Image 불러오기
  6. Grid 설정하기
  7. PhotoView 설정해서 사진 보여주기

결과물


grid는 최선이었다...

깃헙 링크

💡 SwiftData에는 Image 말고 Data로 이미지를 저장했는데, 이를 화면에 나타내기 위해서 Data에서 Image로 변환하기 위해서 extension을 설정했다.
SwiftUI Image from data

❗️ 아래 변수를 다른 이름으로 바꾸면 에러(failed to find a currently active container for Photo)가 난다.

@Attribute(.externalStorage) var photo: Data
profile
계속 해보자

0개의 댓글

관련 채용 정보