Skip to content
The Chat Archive
claude-1· Anthropic Arena Human Preference 55K

write an image upscaling view using SwiftUI

  1. User

    write an image upscaling view using SwiftUI

  2. Assistant

    Here's a SwiftUI view that upscales an image:

    struct UpscaledImageView: View {
        let image: Image
        let scale: CGFloat
        
        var body: some View {
            image
                .resizable()
                .interpolation(.linear)
                .scaledToFit()
                .frame(width: image.size.width * scale, height: image.size.height * scale)
        }
    }
    
    struct UpscaledImageView_Previews: PreviewProvider {
        static var previews: some View {
            UpscaledImageView(image: Image("example"), scale: 3.0)
        }
    }
    

    This view:

    • Takes an Image and a scale factor
    • Uses .resizable() to make the image resizable
    • Uses .interpolation(.linear) to use linear interpolation when resizing
    • Uses .scaledToFit() to scale the image to fit the frame while preserving aspect ratio
    • Sets a frame size that is the original size multiplied by the scale factor
    • The result is an upscaled version of the image by the given scale factor

    You can see a preview of this in action in the UpscaledImageView_Previews struct. Let me know if you have any other questions!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.