Replacing [[UIScreen mainScreen] scale] for visionOS

The [[UIScreen mainScreen] scale] API is unavailable for visionOS, but I use it in various places to pass into Apple's drawing APIs ... for e.g.:

UIGraphicsBeginImageContextWithOptions(self.size, NO, [[UIScreen mainScreen] scale]);

and for generating thumbnails:

QLThumbnailGenerationRequest *request = [[QLThumbnailGenerationRequest alloc] initWithFileAtURL:fileURL size:size scale:screenScale representationTypes:QLThumbnailGenerationRequestRepresentationTypeThumbnail];

What would be the best way to get the 'scale' factor in this case, when building for visionOS?

Answered by Frameworks Engineer in 758565022

You can pass 0.0 for the scale when calling UIGraphicsBeginImageContextWithOptions and it will determine the scale itself, although you should likely favor UIGraphicsImageRenderer for these tasks as you go forward.

In general however, you shouldn't rely upon doing these kinds of tasks without knowing the destination you are drawing to (e.g. a view) as you may end up drawing something that is at the wrong scale anyway. UITraitCollection.displayScale is the preferred means of getting the scale that you should be drawing content at for any given view.

Accepted Answer

You can pass 0.0 for the scale when calling UIGraphicsBeginImageContextWithOptions and it will determine the scale itself, although you should likely favor UIGraphicsImageRenderer for these tasks as you go forward.

In general however, you shouldn't rely upon doing these kinds of tasks without knowing the destination you are drawing to (e.g. a view) as you may end up drawing something that is at the wrong scale anyway. UITraitCollection.displayScale is the preferred means of getting the scale that you should be drawing content at for any given view.

Replacing [[UIScreen mainScreen] scale] for visionOS
 
 
Q