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

Status bar color and image orientation issues #452

Open
Shouheng88 opened this issue Jun 25, 2024 · 8 comments
Open

Status bar color and image orientation issues #452

Shouheng88 opened this issue Jun 25, 2024 · 8 comments

Comments

@Shouheng88
Copy link

First of all, thanks for your work for this library.

When I tried to use this library in SwiftUI in my App, I found two issues:

  1. Status Bar Color

When exit from the photo browser, the status bar didn't recover. The photo browser changed the status bar color to white, but when exit, it didn't change it back to black.

  1. Image Orientation

Image captured from camera is displayed and rotated with 90 degrees.

I think these fixing theses issues will make the library better. Thanks!

@tommyming
Copy link
Collaborator

Hello @Shouheng88, thanks for the raise out of the issues.

As far as I know, this library is mainly developed using UIKit, so suppose you will need UIViewRepresentable to bridge to SwiftUI.
Therefore, there will be some unexpected behaviours. I can try to investigate to see if there is any workaround on this.

For Image orientation, would you please provide some reference e.g. reproducable project that we can investigate? Thanks.

As always, feel free to submit any pull requests if you are willing to do so!

@Shouheng88
Copy link
Author

Hi @tommyming, thanks for your reply.

I fixed the status bar color issue by chaning the background color. Now it works well.

For the image orientation issue. As you can see in the below two images. The first one is the image diplayed by Kingfisher, a image library of SwiftUI. And the second one is the image displayed in SKPhotoBrowser.

I used to meet this problem when I program with Android. In Android, we need to read the image orientation from the EXIF information. And when display the image, we need to change the orientation accordingly.

fun File.orientation(): Int {
    try {
        val exif = ExifInterface(absolutePath)
        return when (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0)) {
            6 -> 90
            3 -> 180
            8 -> 270
            else -> 0
        }
    } catch (e: IOException) {
        e.printStackTrace()
    }
    return 0
}

I'd love to contribute code. But I'm new to iOS development. I'll try to find a solution.

I hope the information will help! Thanks a lot!

@Shouheng88
Copy link
Author

Shouheng88 commented Jun 26, 2024

It's little strange, the image orientation works well if I use the UIImage instead of url string to get SKPhoto object.

The image from network also works well. Here is my code in SwiftUI

struct ImageBrowseView: View {
    
    @Environment(\.presentationMode)
    private var presentationMode: Binding<PresentationMode>
    @State private var isClosing: Bool = false
    
    private let photos: [SKPhoto]
    private let initIndex: Int
    @State private var index: Int = 0
    
    let urls: [URL]
    
    let url: URL
    
    init(urls: [URL], url: URL) {
        self.urls = urls
        self.url = url
        self.photos = urls.map { url in
            if url.isFileURL {
                SKPhoto.photoWithImage(UIImage(data: try! Data(contentsOf: url))!)
            } else {
                SKPhoto.photoWithImageURL(url.absoluteString)
            }
        }
        var index = 0
        for i in 0..<urls.count {
            if urls[i] == url {
                index = i
            }
        }
        self.initIndex = index
        self.index = index
    }
    
    var body: some View {
        ZStack {
            Color.background_color.ignoresSafeArea(.all)
            PhotoViewer(photos: photos,
                        initIndex: initIndex,
                        index: $index,
                        isClosing: $isClosing)
        }.onChange(of: isClosing, perform: { value in
            if value {
                presentationMode.wrappedValue.dismiss()
            }
        }).navigationBarHidden(true)
    }
}

struct PhotoViewer: UIViewControllerRepresentable {

    let photos: [SKPhoto]
    
    let initIndex: Int
    
    @Binding var index: Int
    
    @Binding var isClosing: Bool

    func makeUIViewController(context: Context) -> SKPhotoBrowser {
        SKPhotoBrowserOptions.indicatorColor = R.color.image_tint_color()!
        SKPhotoBrowserOptions.displayAction = false
        SKPhotoBrowserOptions.displayCloseButton = false
        SKPhotoBrowserOptions.displayHorizontalScrollIndicator = false
        SKPhotoBrowserOptions.displayCounterLabel = false
        SKPhotoBrowserOptions.displayBackAndForwardButton = false
        SKPhotoBrowserOptions.backgroundColor = R.color.background_color()!
        let browser = SKPhotoBrowser(photos: photos)
        browser.initializePageIndex(initIndex)
        browser.delegate = context.coordinator
        return browser
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func updateUIViewController(_ browser: SKPhotoBrowser, context: Context) {
    }

    class Coordinator: NSObject, SKPhotoBrowserDelegate {

        var control: PhotoViewer

        init(_ control: PhotoViewer) {
            self.control = control
        }

        func didShowPhotoAtIndex(_ browser: SKPhotoBrowser, index: Int) {
            self.control.index = index
        }
        
        func willDismissAtPageIndex(_ index: Int) {
            self.control.isClosing = true
        }
    }
}

@tommyming
Copy link
Collaborator

Thanks for the reply, I think it might be the problem of the convenience init methods, I will have a check and update you later.

@tommyming
Copy link
Collaborator

@Shouheng88 sorry for the late update, may I know if the orientation issue happens on the local image?
Since network image and UIImage works, I guess local image should be the only category which is not working.

@Shouheng88
Copy link
Author

Yes, only the local image from camera has this prolbem.

@tommyming
Copy link
Collaborator

@Shouheng88 Hello there, sorry for the long-awaited update.
Checked on the code, suppose the local image is convert to UIImage before showing on the screen.

May I know which version of iOS you are using, and did you set any image orientation methods for the local image that has the issue?

@Shouheng88
Copy link
Author

Thanks for your patience. I'll try the code later.

For questions,

  • I'm using iOS 17.4.1 and iOS 15.0.
  • I didn't set any image orientation.

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

No branches or pull requests

2 participants