swift - 在SwiftUI中根据矩形裁剪图像

标签 swift swiftui

我有一个可以使用 拖动的图像DragGesture() .我想裁剪矩形区域内可见的图像。这是我的代码...

struct CropImage: View {

    @State private var currentPosition: CGSize = .zero
    @State private var newPosition: CGSize = .zero

    var body: some View {
        VStack {
            ZStack {
                Image("test_pic")
                    .resizable()
                    .scaledToFit()
                    .offset(x: self.currentPosition.width, y: self.currentPosition.height)

                Rectangle()
                    .fill(Color.black.opacity(0.3))
                    .frame(width: UIScreen.screenWidth * 0.7 , height: UIScreen.screenHeight/5)
                    .overlay(Rectangle().stroke(Color.white, lineWidth: 3))
            }
            .gesture(DragGesture()
                .onChanged { value in
                    self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height)
            }
            .onEnded { value in
                self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height)

                self.newPosition = self.currentPosition
            })


            Button ( action : {
                // how to crop the image according to rectangle area

            } ) {
                Text("Crop Image")
                    .padding(.all, 10)
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .shadow(color: .gray, radius: 1)
                    .padding(.top, 50)
            }
        }
    }
}

为了更容易理解...

enter image description here

最佳答案

这是使用 .clipShape 的可能方法.使用 Xcode 11.4/iOS 13.4 测试

demo

struct CropFrame: Shape {
    let isActive: Bool
    func path(in rect: CGRect) -> Path {
        guard isActive else { return Path(rect) } // full rect for non active

        let size = CGSize(width: UIScreen.screenWidth * 0.7, height: UIScreen.screenHeight/5)
        let origin = CGPoint(x: rect.midX - size.width / 2, y: rect.midY - size.height / 2)
        return Path(CGRect(origin: origin, size: size).integral)
    }
}

struct CropImage: View {

    @State private var currentPosition: CGSize = .zero
    @State private var newPosition: CGSize = .zero
    @State private var clipped = false

    var body: some View {
        VStack {
            ZStack {
                Image("test_pic")
                    .resizable()
                    .scaledToFit()
                    .offset(x: self.currentPosition.width, y: self.currentPosition.height)

                Rectangle()
                    .fill(Color.black.opacity(0.3))
                    .frame(width: UIScreen.screenWidth * 0.7 , height: UIScreen.screenHeight/5)
                    .overlay(Rectangle().stroke(Color.white, lineWidth: 3))
            }
            .clipShape(
                CropFrame(isActive: clipped)
            )
            .gesture(DragGesture()
                .onChanged { value in
                    self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height)
            }
            .onEnded { value in
                self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height)

                self.newPosition = self.currentPosition
            })


            Button (action : { self.clipped.toggle() }) {
                Text("Crop Image")
                    .padding(.all, 10)
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .shadow(color: .gray, radius: 1)
                    .padding(.top, 50)
            }
        }
    }
}

关于swift - 在SwiftUI中根据矩形裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61305331/

相关文章:

Swift UI 不重绘 NSViewRepresentable 包装的文本字段

ios - SwiftUI - SF 符号不呈现

ios - SwiftUI 无法在 iOS 13 版本中使用 navigationBarTitle 修饰符(已弃用),但可以在 iOS 14 中使用

arrays - swift:比较2个字符数组,从每个字符数组中删除一个值

swift - 本地存储数据的最佳方式是什么(IOS - xcode)

swiftui - 如何在 SwiftUI 中使 UIViewRepresentable 可聚焦在 tvOS 上?

swift - 在 WatchOS 上的 SwiftUI 应用程序中是否可以进行基于页面的导航?

swift - 在 Swift 中访问 superview 的更简洁的方法?

swift - 拥有@objcMembers 私有(private)动态变量时@objc 冗余?

ios - Swift 代码中的 SVPullToRefresh + SVInfiniteScrolling