Post

Replies

Boosts

Views

Activity

Initializing State from another object
I’m trying to build a detail view screen where a user can edit the contents of an object and save it, but I’m struggling to figure out how best to initialize the view. I’m passing in a “MailFilter” object (which I decoded from a web API) to this view (from a List item’s NavigationLink), and am creating state for each of the fields which need to be editable, such that I can use them with a SwiftUI form. I have a custom initializer to set these parameters (and make sure they aren’t nil—as they can be in the actual filter object). struct FilterView: View { var filter: MailFilter @State var from: String @State var to: String init(_ filter: MailFilter) { self.filter = filter from = filter.criteria.from ?? "" to = filter.criteria.to ?? "" } var body: some View { Form { Section(header: Text("From")) { TextField("From", text: $from) } Section(header: Text("To")) { TextField("To", text: $to) } } } } However, this approach doesn’t seem to work. I’m given errors that I’m trying to use self. before initializing the variables—in the initializer! Is there a different approach I should be taking to get my object’s data into this detail view?
4
1
1.9k
Apr ’21
Fullscreen camera preview while using Metal to render?
I've been looking to use some filters on my live camera feed, and have been following guidance of the AVCamFilter sample project - https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/avcamfilter_applying_filters_to_a_capture_stream to use a MTKView to render these live. Within this project, there is some code in PreviewMetalView.swift (in setupTransform()) which scales the X and Y dimensions of the preview down, such that the entire preview is visible on screen: &#9;&#9;&#9;&#9;if textureWidth > 0 &amp;&amp; textureHeight > 0 { &#9;&#9;&#9;&#9;&#9;&#9;switch textureRotation { &#9;&#9;&#9;&#9;&#9;&#9;case .rotate0Degrees, .rotate180Degrees: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;scaleX = Float(internalBounds.width / CGFloat(textureWidth)) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;scaleY = Float(internalBounds.height / CGFloat(textureHeight)) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;case .rotate90Degrees, .rotate270Degrees: &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;scaleX = Float(internalBounds.width / CGFloat(textureHeight)) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;scaleY = Float(internalBounds.height / CGFloat(textureWidth)) &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;// Resize aspect ratio. &#9;&#9;&#9;&#9;resizeAspect = min(scaleX, scaleY) &#9;&#9;&#9;&#9;if scaleX < scaleY { &#9;&#9;&#9;&#9;&#9;&#9;scaleY = scaleX / scaleY &#9;&#9;&#9;&#9;&#9;&#9;scaleX = 1.0 &#9;&#9;&#9;&#9;} else { &#9;&#9;&#9;&#9;&#9;&#9;scaleX = scaleY / scaleX &#9;&#9;&#9;&#9;&#9;&#9;scaleY = 1.0 &#9;&#9;&#9;&#9;} I am, instead, trying to scale the image such that it is zoomed in, filling the view ("Aspect Fill", think like the Snapchat camera view), but, as I'm not familiar with scaling textures for MetalKit to render, I'm not entirely sure how I can scale this such that part of the preview goes beyond the view bounds. What is the best approach to this (I'm imagine this is just some simply multiplication that is alluding me, but would love any help I can get)
1
0
1.5k
Jun ’20