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
Photo
model 만들기
AddPhotoView
만들어서 사진 받아와서 Data로 변환하고 modelContext에 저장하기
name
작성 안 하거나 사진이 없으면 Save 버튼 .disable 설정하기
photos
정렬하기
- Data로 Image 불러오기
- Grid 설정하기
- PhotoView 설정해서 사진 보여주기
결과물
![](https://velog.velcdn.com/images/soaringwave/post/055a1bb0-5cf5-4f72-8857-9c721a691a93/image.gif)
![](https://velog.velcdn.com/images/soaringwave/post/ad3e120c-2ef6-4e2d-966d-4f36f79b359f/image.png)
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