I am engaged on an iOS app, and I’ve an issue with the scannedImage in my SwiftUI view. I am utilizing kontiki’s resolution to seize a snapshot of the view, and it really works tremendous. Here is the display screen of how does it appear like. Nonetheless, after the snapshot is taken, the scannedImage modifications its body measurement, it is resizing to the underside of the view, and this habits solely occurs as soon as. Additionally it is solely taking place on the backside, as you’ll be able to see within the second hyperlink, it is efficiently cropping it if I am positing my signature on left and proper locations and urgent inexperienced. I am unsure why that is taking place, I’ve tried to catch the time when scannedImage measurement is altering, however no luck. Hope you’ll be able to assist with it. Listed below are the related components of my code:
// Fundamental half that capturing the snapshot
non-public func captureSnapshot(rect: CGRect) -> UIImage? {
var end result: UIImage?
if let scene = UIApplication.shared.connectedScenes.first(
the place: { $0.activationState == .foregroundActive }
) as? UIWindowScene {
end result = scene.home windows[0].rootViewController?.view.asImage(rect: rect)
}
return end result
}
extension UIView {
func asImage(rect: CGRect) -> UIImage {
let renderer = UIGraphicsImageRenderer(bounds: rect)
return renderer.picture { rendererContext in
layer.render(in: rendererContext.cgContext)
}
}
}
// View itself
var physique: some View {
VStack {
GeometryReader { geometry in
let magnificationGesture = MagnificationGesture()
.onChanged{ gesture in
scaleAnchor = .heart
scale = lastScale * gesture
}
.onEnded { _ in
fixOffsetAndScale(geometry: geometry)
}
let dragGesture = DragGesture()
.onChanged { gesture in
var newOffset = lastOffset
newOffset.width += gesture.translation.width
newOffset.top += gesture.translation.top
offset = newOffset
}
.onEnded { _ in
fixOffsetAndScale(geometry: geometry)
}
Picture(uiImage: scannedImage)
.resizable()
.scaledToFit()
.border(.grey, width: 2)
.scaleEffect(scale, anchor: scaleAnchor)
.offset(offset)
.gesture(dragGesture)
.gesture(magnificationGesture)
.overlay (
VStack {
ZStack {
ZStack(alignment: .bottomTrailing) {
Rectangle()
.stroke(model: StrokeStyle(lineWidth: 2, sprint: [5]))
.fill(.blue)
.opacity(opacity)
Rectangle()
.fill(.clear)
Picture(uiImage: signImage)
.resizable()
.scaledToFit()
VStack {
HStack {
Spacer()
Circle()
.fill(Shade.inexperienced)
.body(width: 25, top: 25)
.onTapGesture {
opacity = 0.0
DispatchQueue.essential.asyncAfter(deadline: .now() + 0.3, execute: {
scannedImage = captureSnapshot(rect: geometry.body(in: .international))!
opacity = 1.0
})
}
}
Spacer()
}
.opacity(opacity)
.zIndex(2)
}
.body(width: width, top: top)
}
}
.body(maxWidth: width, maxHeight: top, alignment: .heart)
.place(location)
.gesture(
simpleDrag.concurrently(with: fingerDrag)
)
)
Spacer()
// .body(width: geometry.measurement.width, top: geometry.measurement.top)
}
}
.onChange(of: scannedImage.measurement.top, carry out: { newValue in
print("top modified, however why?!")
})
.background(Shade.black.opacity(0.3).ignoresSafeArea(.all))
}
My guess is that the issue is with captureSnapshot
, It is getting extra, then It is purported to. Hope you’ll be able to assist with it, thanks.
EDIT: The Spacer()
is just not the issue.