suzuki-0000 / SKPhotoBrowser

Simple PhotoBrowser/Viewer inspired by facebook, twitter photo browsers written by swift
MIT License
2.61k stars 532 forks source link

Status bar color and image orientation issues #452

Open Shouheng88 opened 1 week ago

Shouheng88 commented 1 week ago

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 commented 1 week ago

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 commented 1 week ago

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 commented 1 week ago

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 commented 6 days ago

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.