Drawing UIImage with perspective on Apple Watch

I have an Apple Watch app (Swift), and I need to draw a UIImage with perspective, preferably into a new UIImage (see example below). The best way Apple provides is by performing a transform on UIView. Unfortunately, UIView is NOT available in WatchKit.

Does anyone know how this can be accomplished? I've been searching for weeks, but have not found a solution. CoreImage seems to have promise, but parts of it are not available in WatchKit.

Cannot be done. I even tried using ChatGPT to create code, but it tries to use some Core Image calls (even though I told it not to), which are not available on Apple Watch. I'm trying to do it now via brute force, with marginal success so far.

Effectively, transform would have been a great way to do this. https://stackoverflow.com/questions/57477164/how-can-i-use-cgaffinetransform-to-skew-a-uiview-in-multiple-directions-for-exa

But you could probably do it "manually". I have not tried, but logic would be:

  • define the scale for top (topScale 0.8 for instance) and scale for bottom (bottomScale 1.2 for instance)
  • split the image in horizontal parts (1 or a few pixels high)
  • save in an array
  • for each item in array compute a new image by scaling horizontally with scaleFactor
  • scaleFactor is computed as linear progression from topScale to bottomScale depending on the line
  • assemble the new array in an image

That should be the UIImage with perspective.

If that works, you should post the code here.

Thanks, Claude31. I tried something along those lines, but the problem turns out to be even more complex. Because it is perspective, the pixels at the top of the image actually need to become smaller because they appear "further away" from the viewer, and the pixels at the bottom of the image become larger because they appear "closer". This means the height of each input and output row is different. I've gotten pretty close, but it still doesn't look quite right. I'm continuing to work on it...

Drawing UIImage with perspective on Apple Watch
 
 
Q