iOS Development

ios – SwiftUI – Tips on how to crop the unique picture (utilizing CGImage) after scaling and transferring it?

I have been making an attempt to unravel the issue for a very long time, however I am unable to. I’ll actually respect your assist!
Proper now I am fighting figuring out the coordinates (Offset) of the primary level of the a part of the picture I am seeing on the display.

For instance, this code prints the offset after the picture has been moved. And I do not perceive make it in order that when the picture is scaled, the offset adjustments accordingly. I imply that if I wish to crop a photograph from the identical place, for instance on the left edge, however at totally different scales, I am unable to do this right here, as a result of the offset coordinates aren’t the identical.

struct ContentView: View {
    @State non-public var imageScale: CGFloat = 1.0
    @State non-public var previousScale: CGFloat = 0
    @State non-public var imageOffset: CGSize = .zero
    @State non-public var previousOffset: CGSize = .zero

    var physique: some View {
        let dragGesture = DragGesture()
            .onChanged { worth in
                self.imageOffset.width = worth.translation.width + self.previousOffset.width
                self.imageOffset.top = worth.translation.top + self.previousOffset.top
            }
            .onEnded { worth in
                self.previousOffset = self.imageOffset

                print("Offset: (imageOffset)")
            }

        let pinchGesture = MagnificationGesture()
            .onChanged { scale in
                imageScale = scale + previousScale
            }
            .onEnded { _ in
                previousScale = imageScale - 1
            }

        Picture(systemName: "particular person.fill")
            .resizable()
            .scaledToFill()
        .scaleEffect(imageScale)
        .offset(imageOffset)
        .gesture(dragGesture)
        .gesture(pinchGesture)
    }
}

Credit: www.ismmailgsm.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button