SwiftUI - MacOS - 在图像上绘制一个矩形进行裁剪

问题描述

我想用矩形裁剪图像。

我正在显示一个图像,用户将在图像中的任意位置拖动一个矩形。

它将给出一个新图像,对应于用户绘制矩形的图像部分。

我设法显示图像并绘制了一个矩形。

现在我需要将拖动手势位置恢复到原始图像以进行裁剪,但我找不到链接

我现在有这个。

var body: some View {
    vstack {
        
        Text("Drag a rectangle around the graph to crop the image").padding()

        GeometryReader { geo in
            ZStack {
                
                Image(nsImage: NSImage(cgImage: inputimage,size: NSSize(width: inputimage.width,height: inputimage.height)))
                    .resizable()
                    .scaledToFit()
                    .gesture(DragGesture(minimumdistance: 0,coordinateSpace: .global).onChanged { dragGesture in
                       
                        print("START = \(dragGesture.startLocation)")
                        print(" END  = \(dragGesture.location)")
                        print("G - GLOBAL = \(geo.frame(in: .global))")
                        print("G - GLOBAL MAXX = \(geo.frame(in: .global).maxX)")
                        print("G - GLOBAL MINX = \(geo.frame(in: .global).minX)")
                        print("G - GLOBAL MIDX = \(geo.frame(in: .global).midX)")
                        print("G - GLOBAL MAXY = \(geo.frame(in: .global).maxY)")
                        print("G - GLOBAL MINY = \(geo.frame(in: .global).minY)")
                        print("G - GLOBAL MIDY = \(geo.frame(in: .global).midY)")
                        print("G - LOCAL  = \(geo.frame(in: .local))")
                        print("INMG = \(inputimage.width) * \(inputimage.height)")
                        
                        positionX = dragGesture.startLocation.x - geo.frame(in: .global).minX
                        positionY = geo.frame(in: .global).size.height - dragGesture.startLocation.y + geo.frame(in: .global).minY
                        
                        selectionWidth = 1 + abs(dragGesture.startLocation.x - dragGesture.location.x)
                        offsetX = selectionWidth / 2
                        
                        selectionHeight = 1 + abs(dragGesture.startLocation.y - dragGesture.location.y)
                        offsetY = selectionHeight / 2
                        
                        cropWidth = (selectionWidth / geo.frame(in: .global).minX) * CGFloat(inputimage.width)
                        print("X CROP = \(cropWidth)")
                        
                        cropHeight = (selectionHeight / geo.frame(in: .global).height) * CGFloat(inputimage.height)
                        print("Y CROP = \(cropHeight)")
                        
                        let extraWidthSide: CGFloat = ((geo.frame(in: .global).width - geo.frame(in: .global).minX) / 2)
                        print("EXTRA WIDE = \(extraWidthSide)")
                        

                        cropPositionX =  max(0,((dragGesture.startLocation.x - geo.frame(in: .global).minX - extraWidthSide) / geo.frame(in: .global).minX) * CGFloat(inputimage.width))
                        print("CROP X POS = \(cropPositionX)")
                            

                        cropPositionY = CGFloat(inputimage.height) -
                                (((dragGesture.startLocation.y - geo.frame(in: .global).minY) / geo.frame(in: .global).height) * CGFloat(inputimage.height))
                        print("CROP Y POS = \(cropPositionY)")

                        
                    })
                    .border(Color.black)
                
                    Rectangle()
                        .border(Color.blue,width: 3)
                        .frame(width: selectionWidth,height: selectionHeight)
                        .foregroundColor(Color.clear)
                        .position(x: positionX,y: positionY)
                        .offset(x: offsetX,y: offsetY)
            }
            
        }
        .frame(width: 600,height: 400)
        .padding()
        
        
        
        if let result = resultimage {
            Image(nsImage: NSImage(cgImage: result.cropping(to: CGRect(x: cropPositionX,y: cropPositionY,width: cropWidth,height: cropHeight))!,height: inputimage.height)))
                .resizable()
                .scaledToFit()
                .border(Color.purple,width: 2)
                .frame(width: 600,height: 400)
        }
        
        
        HStack {
            Spacer()
            Button("dismiss") {
                presentationMode.wrappedValue.dismiss()
            }.mainButton()
            
            Spacer()
            Button("Crop") {
                resultimage = inputimage.cropping(to: CGRect(x: cropPositionX,height: cropHeight))
                presentationMode.wrappedValue.dismiss()
            }.mainButton()
            
            Spacer()
        }
    }
    .frame(width: 1400,height: 1000)

它“适用于”横向图像,但不适用于纵向。

如果您有解决方案,或者您知道可以解决此问题的软件包。我会接受一切。

谢谢,

尼古拉斯

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)