This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Hi everyone.
Been bashing my head against this problem for a couple days now and could use some input. I have an ItemDetail view which loads Core Data. My Item has a .image property which holds binary data, that I convert into images.
I followed a guide to create a PhotoPickerModel. The photos chosen from the photo library are stored in a custom class PickedMediaItems:
//Class to hold picked photos from photo picker
class PickedMediaItems: ObservableObject {
@Published var items = [PhotoPickerModel]()
func append(item: PhotoPickerModel) {
items.append(item)
}
func deleteAll() {
for (index, _) in items.enumerated() {
items[index].delete()
}
items.removeAll()
}
}
In my ItemDetail view:
@ObservedObject var item: Item
@ObservedObject var mediaItems = PickedMediaItems()
List(mediaItems.items, id: \.id) { item in
ZStack(alignment: .topLeading) {
if item.mediaType == .photo {
Image(uiImage: item.photo ?? UIImage())
.resizable()
.aspectRatio(contentMode: .fit)
} else {
}
Image(systemName: getMediaImageName(using: item))
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 24, height: 24)
.padding(4)
.background(Color.black.opacity(0.5))
.foregroundColor(.white)
}
}.frame(height: 300)
However, I want my view to work more like this:
List(PersistenceController.shared.imagesFromCoreData(object: item.image)!.items
, id: \.id) { item in
ZStack(alignment: .topLeading) {
if item.mediaType == .photo {
Image(uiImage: item.photo ?? UIImage())
.resizable()
.aspectRatio(contentMode: .fit)
} else {
//crash app if live photo is added
}
Image(systemName: getMediaImageName(using: item))
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 24, height: 24)
.padding(4)
.background(Color.black.opacity(0.5))
.foregroundColor(.white)
}
}.frame(height: 300)
where it loads the stored binary data and converts it to the list of stored images. I have a function imagesFromCoreData that does this.
My primary issue right now is that if I am creating the list initially using this function, I do not have a dynamic list of photos.
Ideally I would want to initially set var mediaItems = to the converted binary data, therefore I can just work with mediaItems throughout the view until I save it and convert it back to binary data. This obviously can't be done in the view, but I'm struggling to figure out how to properly achieve this. Any insight would be greatly appreciated!
Post Details
- Posted
- 3 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/SwiftUI/com...