media.selectPhoto()

Type Function
Library media.*
Return value None
Revision 2014.2277
Keywords media, camera, photo library
Sample code /CoronaSDK/SampleCode/Media/Camera
/CoronaSDK/SampleCode/Media/PhotoPicker
See also media.hasSource()

Overview

Opens a platform-specific interface to the device's photo library. This function is asynchronous, i.e. it returns immediately so the calling code will continue to execute until the end of its scope; after that, the application will be suspended until the session is complete. By default, the image object is added to the top of the current stage (there is an option to save the image to a directory instead). A listener is required to handle the returned display object (image).

Gotchas

Windows

The Corona Simulator for Windows does not support this API and it will do nothing when calling this function.

iOS

The iPad requires additional parameters, origin and permittedArrowDirections, to specify the location and direction of the popover used to select the photo.

Syntax

media.selectPhoto
({
    listener = myListener
    [, mediaSource]
    [, destination]
    [, origin]
    [, permittedArrowDirections]
})
listener (required)

Listener. Can be either a function listener or a table listener. If a table, it must have a completion method. The event dispatched to the listener will be a completion event with the following additional properties:

  • event.target is a DisplayObject based on the mediaSource parameter. If the chosen image is saved to a file, there is no display object added to the stage and this value will be nil.
  • event.completed will be true if the user selected a photo; false if the user cancelled the camera or photo selection.
mediaSource (optional)

Constant. Can be one of the following:

  • media.PhotoLibrary
  • media.SavedPhotosAlbum
destination (optional)

Table. If provided, the chosen image is saved to a file. In this case, there is no DisplayObject added to the stage. The parameter filetable is a table of the form { baseDir=, filename= [, type=] }. For the 'type' property, use an appropriate MIME type such as "image". This is useful if you want to save the full-resolution of the chosen image to a file. (Only available on iOS and Android).

origin (optional)

Table. The rectangle of the button which the iPad's popover emerges from. A convenience programming pattern is to pass the contentBounds of your button. (Only available on iPad.)

media.selectPhoto( { listener = sessionComplete, origin = myButton.contentBounds } )
permittedArrowDirections (optional)

Table. An optional field that is an array of allowed directions the iPad's popover arrow may point. Valid values are "up", "down", "left", "right", "any". The default is "any". (Only available on iPad.)

media.selectPhoto( { listener = sessionComplete, origin = myButton.contentBounds, permittedArrowDirections = { "up", "down" } } )

Example

local function onComplete(event)
   local photo = event.target
   print( "photo w,h = " .. photo.width .. "," .. photo.height )
end

if media.hasSource( media.PhotoLibrary ) then
   media.selectPhoto( { mediaSource = media.PhotoLibrary, listener = onComplete } )
else
   native.showAlert( "Corona", "This device does not have a photo library.", { "OK" } )
end
-- Examples using Photo Library for iPad

-- Completion listener
local function onComplete(event)
   local photo = event.target

   print( "onComplete called ..." )

   if photo then
       print( "photo w,h = " .. photo.width .. "," .. photo.height )
    end
end

local button = display.newRect(120,240,80,70)

-- Button tap listener
local function pickPhoto( event )

    -- Note: Only use one of the media.show routines listed below

    -- Save photo to file in Temporary directory
--  media.selectPhoto( 
--      {
--          mediaSource = media.PhotoLibrary,
--          listener = onComplete, 
--          origin = button.contentBounds, 
--          permittedArrowDirections = { "right" }, 
--          destination = {baseDir=system.TemporaryDirectory, filename="image.jpg", type="image"} 
--      } )

    -- Show photo on screen (no file save)  
    media.selectPhoto(
        {
            mediaSource = media.SavedPhotosAlbum,
            listener = onComplete, 
            origin = button.contentBounds, 
            permittedArrowDirections = { "right" } 
        } )
end

button:addEventListener("tap", pickPhoto )