Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use PreferenceKeys to pass images up the view hieararchy. #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

maciekish
Copy link

Hi,
Thanks for making this great library. I have used it a lot but always face the same issue. I have a complex view hiearachy and more often than not the image i want to display lives in code several view hiearchies away from the nearest NavigationView. This makes it a pain to pass it upp all the way using $bindings. So i created some support that uses PreferenceKeys to bubble the image up the view hiearchy.

Usage

Where you have your image for display, you would use the .viewImage(image: UIImage?) modifier to request an image to be displayed like so:

Image(uiImage: image)
.onTapGesture {
    viewImage = image
}
.viewImage(image: viewImage)

And anywhere higher up in the view hiearchy (in a different file) you can just attach the .imageViewier() modifier to display that image.

NavigationView {
    ...
}
.imageViewier()

And that's it. No more passing images around manually.

/// Allows this view to display images as requested by child views further down in the view hierarchy, without having to manually pass the image throught the view hierarchy.
///
/// For example, you can attach this modifier to a `NavigationView` and use the `viewImage(image: UIImage?)`modifier on any view further down in the hierarchy.
public func imageViewier() -> some View {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo, change imageViewier to imageViewer.

@Jake-Short
Copy link
Owner

Thanks for working on this addition, it looks great so far! One comment I left above is just a small typo (imageViewier). Another change I think would be good is to use the SwiftUI Image rather than UIImage to stay consistent with using Image elsewhere in the package.

Copy link
Owner

@Jake-Short Jake-Short left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix typo (see comment) and change UIImage to Image

@X901
Copy link

X901 commented Jul 24, 2022

This is a great idea!
after I tried I found some bugs,

Sometime it will open the image after scrolling ! not when I tap on the image
Always if I tap on image once, it won't open again
what I mean is it will open first time only !

the great think about this solution, I can get the image from cardView
then show ImageViewer inside TapView, that will solve issue Imageviewer won't appear overlay TabView !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants