FWIW, my idea was to translate the CGPoint in the scribbles from 0.0-1.0 of the bounds so that the scribbles could be rendered regardless of resolution (e.g. you rotate device, you go split screen on iPad, etc.). And then you’d multiply those by 240 for serialization.
But in my main project when I send the canvas from a screen to another screen and at the end I save the report in DB after I get the CGPoints back and rescale the CGPoints, these will be in a different place.
In scaleCGPoints you pass the CanvasView (I might just pass the bounds, but it achieves the same thing). I’d do the same thing in parseScribbleData. And I’d do a test where I pass the scribbles, convert to UInt8 with one function and convert back with another function and make sure the values were close (they’re going to be slightly different because that UInt8 structure throws away valuable information, but you’re stuck with that).
But the serialize and deserialize functions should do all of the factor adjustments (not trying to clean it up later).
And multiply by factor in one and divide in the other and confirm that the factor for both was the same.
Now, all of this assumes that the bounds.width, for example, is the same during capture and review.
So, when you serialize and deserialize, how do the values of input to serialize and the output from deserialize compare? (Just look at the first few values and any issues should jump out at you.)
This will help you isolate the problem. Is it the serialization/deserialization? Or is it the rendering on the canvas?
When I serialize I have this values in array (using only a simple dot somewhere on the screen ): Stroking true: [(124.33332824707031, 27.0), (124.33332824707031, 27.0)]
> Same width and height and also same aspect ratio: 240:127
Fine, same ratio. But is the actual width the same?
OK, those values look like the serialization isn’t likely the issue. So I question whether the actual width of the two canvases (not just the aspect ratios, but the resulting widths) are the same.
If I click the row 2 times will refresh the canvas and I will get the CGPoints in almost the same position
But I want that CGPoints to be there from when I review the report.
This is why I think is because I don't rescale the CGPoints in the right place.
Im doing the rescale in a function called in the cellForRowAt
Which is called when the cell is refreshed
Not when the view is loaded.
And all of this because I'm using that canvas parameter to that function. And I don't know how to replace it or to do the rescaling when I deserialise.
Do you think you can help me to rescale the CGPoints here?
guard bytes.count % 2 == 0 else { print("The scribble data is corrupted: odd number of bytes") return [] }
var result = [Scribble]() var current: Scribble? = nil var index = 0
while index <= bytes.count - 2 { let x = bytes[index] let y = bytes[index + 1] let hasMarker = y & Scribble.yMarkerBitmask == Scribble.yMarkerBitmask let point = CGPoint(x: CGFloat(x), y: CGFloat(y & ~Scribble.yMarkerBitmask))
It looks like two problems. One, your initial render is off. Second, even after you click on the second time, they’re not close to where they should be.
So, you’re not applying the factor during this deserialization. You’re probably getting CGPoint that have values between 0 and 240, I would have thought.
You are rescaling in cellForRowAt. But the frame of the cell when it’s first presented won’t be right at that point (not the first time you call it, anyway). Constraints haven’t been applied, IIRC.
In cellForRowAt, print cell.reviewCanvas.bounds. I bet when you call it the first time and when you call it the second time, you’ll have different values.
I bet the first time you call it you’re probably getting whatever size it was in the storyboard (not adjusted for the user’s actual device). But when you select row second time (and it reloads the cell), the constraint has been applied.
This is precisely the problem I was thinking I was thinking you could solve by refactoring this so the serialization and deserialization should just multiply and divide by 240, respectively, and then do the subsequent scaling by bounds in the canvas view.
This still hardcodes assumptions about the fact that serialization expects 0-240 for x and 0-127 for y, that the scale of the image view happens to match, that serializer loses precision of touches, etc. All of that is not great. But hopefully it should at least work and you can work on it from here.