Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

Can you write code for Codea that allows the user to access their photo library and…

  1. User

    Can you write code for Codea that allows the user to access their photo library and display a picture

  2. Assistant

    Certainly! Below is an example code in Codea that allows the user to access their photo library and display a selected picture:

    -- Global variables
    local selectedImage = nil
    
    function setup()
        -- Enable the photo library
        parameter.boolean("ChooseImage", false, imagePicker)
    end
    
    function draw()
        background(40, 40, 50)
        
        -- Draw the selected image (if any)
        if selectedImage then
            sprite(selectedImage, WIDTH/2, HEIGHT/2)
        else
            fill(255)
            text("Tap 'Choose Image' to pick a photo", WIDTH/2, HEIGHT/2)
        end
    end
    
    function imagePicker(value)
        if value then
            -- Open photo library dialog
            PhotoPicker.open(function(image)
                if image then
                    selectedImage = image
                end
            end)
        end
    end
    

    You can copy this code into a new project in Codea and run it. Once you tap the "Choose Image" checkbox, a dialog will open allowing you to select an image from your photo library. The selected image will then be displayed on the screen.