简体   繁体   English

非矩形图像裁剪 SwiftUI?

[英]Non-rectangular image cropping SwiftUI?

I would like to allow a user to crop images by drawing a closed shape on the original image.我想允许用户通过在原始图像上绘制闭合形状来裁剪图像。 Sort of like Snapchat stickers.有点像 Snapchat 贴纸。 I was looking at this post from 2012 and was curious if there is an updated way to do this with SwiftUI.我从 2012 年开始查看这篇文章,并且很好奇是否有更新的方法可以使用 SwiftUI 做到这一点。

To clarify, I would like to take user input (drawing) over a displayed image and crop the actual image (not the view) with a mask that is that shape.为了澄清,我想在显示的图像上获取用户输入(绘图),并使用该形状的蒙版裁剪实际图像(而不是视图)。

Actually your question need to more specific.其实你的问题需要更具体。

but you can resize your image by following code in swift ui.但是您可以通过 swift ui 中的代码来调整图像大小。

Image("Your Image name here!")
    .resizable()
    .frame(width: 300, height: 300)
    

if you want to crop at any position of the image you may try this one如果您想在图像的任何 position 处进行裁剪,您可以试试这个

import SwiftUI

struct ContentView: View {
    var body: some View {
        
        CropImage(imageName: "Your Image name here!")
        
    }
}

// struct Shape
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.main.bounds.size.width*0.7, height: UIScreen.main.bounds.size.height/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 image crop
struct CropImage: View {
    
    let imageName: String
    
    @State private var currentPosition: CGSize = .zero
    @State private var newPosition: CGSize = .zero
    @State private var clipped = false
    
    var body: some View {
        
        VStack {
            
            ZStack {
                
                Image(imageName)
                    .resizable()
                    .scaledToFit()
                    .offset(x: currentPosition.width, y: currentPosition.height)
                
                Rectangle()
                    .fill(Color.black.opacity(0.3))
                    .frame(width: UIScreen.main.bounds.size.width*0.7 , height: UIScreen.main.bounds.size.height/5)
                    .overlay(Rectangle().stroke(Color.white, lineWidth: 3))
            }
            .clipShape(
                CropFrame(isActive: clipped)
            )
            .gesture(DragGesture()
                        .onChanged { value in
                            currentPosition = CGSize(width: value.translation.width + newPosition.width, height: value.translation.height + newPosition.height)
                        }
                        .onEnded { value in
                            currentPosition = CGSize(width: value.translation.width + newPosition.width, height: value.translation.height + newPosition.height)
                            
                            newPosition = currentPosition
                        })
            
            
            Button (action : { clipped.toggle() }) {
                
                Text("Crop Image")
                    .padding(.all, 10)
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .shadow(color: .gray, radius: 1)
                    .padding(.top, 50)
                
            }
        }
    }
}

This is the full code of a view with image cropping.这是带有图像裁剪的视图的完整代码。

else you may use a library from GitHub, see the GitHub demo here否则您可以使用 GitHub 中的库,请参阅此处的 GitHub 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM