Swiftpack.co - luxxyarns/ImageLayouter as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by luxxyarns.
luxxyarns/ImageLayouter 1.0.2
Tool to layout images in a rectangle using binary tree based bin packing and additional growth adjustment
⭐️ 0
🕓 1 year ago
iOS macOS
.package(url: "https://github.com/luxxyarns/ImageLayouter.git", from: "1.0.2")

ImageLayouter

Tool to layout images in a rectangle using binary tree based bin packing and additional growth adjustment

screenshot 2

Sample usage


import SwiftUI
import ImageLayouter

struct ContentView: View {
    var imageNames = [   "w5", "w6", "w7", "w8", "w9", "w1", "w2", "w3","w4", ].shuffled()
    @State var resultingImage: UIImage?
    @State var ratio: Double = 1
    
    func reload() {
        ImageLayouter.layout(border: 10, images: self.imageNames.map({ name in
            if let image = UIImage(named: name) {
                return image
            } else {
                return UIImage()
            }
        }), ratio: self.ratio) { image in
            if let image = image {
                DispatchQueue.main.async {
                    self.resultingImage = image
                }
            }
        }
    }
    
    var body: some View {
        GeometryReader { reader in
            VStack {
                ZStack {
                    if let image = resultingImage {
                        Image(uiImage: image)
                            .resizable()
                            .scaledToFill()
                        
                    } else {
                        Text("processing ...")
                            .onAppear() {
                                self.ratio = reader.size.width / reader.size.height
                                self.reload()
                            }
                    }
                    VStack {
                        Spacer()
                        HStack {
                            Spacer()
                            Button {
                                self.reload()
                            } label: {
                                Image(systemName: "arrow.clockwise")
                            }
                            .buttonStyle(.borderedProminent)
                            .padding()
                        }
                    }
                }
                .onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { newValue in
                    let w = reader.size.width
                    let h = reader.size.height
                    if UIDevice.current.orientation.isLandscape {
                        ratio = w > h ? w / h : h / w
                    }
                    if UIDevice.current.orientation.isPortrait {
                        ratio = w < h ? w / h : h / w
                    }
                    self.reload()
                }
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
}

screenshot 1

GitHub

link
Stars: 0
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics