HomeiOS Developmentios - Swiftui picture cropper and rotate does not work

ios – Swiftui picture cropper and rotate does not work


import SwiftUI

struct ContentView: View {
    @State personal var rotationAngle: Double = 0
    @State personal var cropRect = CGRect(x: 0, y: 0, width: 200, top: 200)
    @State var picture: UIImage
    var physique: some View {
        VStack {
            Button(motion: {
                withAnimation {
                    rotationAngle += 90
                }
            }) {
                Textual content("Rotate")
            }
            Button(motion: {
                withAnimation {

                    let cropRects = CGRect(
                        x: cropRect.origin.x, y: cropRect.origin.y, width: cropRect.maxX,
                        top: cropRect.maxY)
                    let cgImage = picture.cgImage?.cropping(to: cropRects)
                    let croppedImage = UIImage(cgImage: cgImage!)
                    picture = croppedImage
                }
            }) {
                Textual content("Crop")
            }
            ZStack {
                GeometryReader { geo in
                    Picture(uiImage: picture)
                        .resizable()
                        .rotationEffect(.levels(rotationAngle))
                    Rectangle()
                        .stroke(Colour.white, lineWidth: 2)
                        .background(Colour.black.opacity(0.8))
                        .place(x: self.cropRect.midX, y: self.cropRect.midY)
                        .body(width: cropRect.dimension.width, top: cropRect.dimension.top)
                        //          .scaledToFit()
                        .rotationEffect(.levels(rotationAngle))

                        .gesture(
                            DragGesture()
                                .onChanged { worth in
                                    let x = worth.location.x - (self.cropRect.width / 2)
                                    let y = worth.location.y - (self.cropRect.top / 2)
                                    let maxX = geo.dimension.width - self.cropRect.width
                                    let maxY = geo.dimension.top - self.cropRect.top

                                    self.cropRect.origin.x = min(max(x, 0), maxX)
                                    self.cropRect.origin.y = min(max(y, 0), maxY)

                                }
                        ).onAppear {
                            cropRect = CGRect(
                                x: 0, y: 0, width: geo.dimension.width, top: geo.dimension.top)
                        }
                }
            }.body(top: 500)
        }
    }
}

I’ve tried to create picture cropper to crop and rotate picture, nevertheless I’ve obtained three issues with the code:

  1. if I rotate the picture the picture might be cropped with the view background(the picture grow to be shorter in top so the crop is now out of bounds)
  2. the crop does not seize the rectangle place
  3. The rectangle cant be resized

some ways however nothing labored`

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments