Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to use a fucntion to resize an image (found in a tutorial) for display.loadRemoteImage?
Started by GO4IT May 09 2019 05:54 AM

3 replies to this topic
resize image loadremoteimage api

Best Answer GrahamRanson , 09 May 2019 - 07:28 AM

Inside the networkListener function there is a table passed in called "event", on this table is a variable called "target" - this is your display object.

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

GO4IT

[GLOBAL: userInfoPane.html]
GO4IT
  • Observer

  • 15 posts
  • Corona SDK

Hello, I have a question for resizing an image. The tutorial to fit images to a size is great to work on display.newImage(), but I cannot manage to do it for display.loadRemoteImage(). I think it because display.loadRemoteImage() does not return DisplayObject which is required for this resize function.

How can I resize a remote image like below?

 

In my case, I am using an image URL which is a result of API call (network.request). I store it as "image.png". So far, I can display loadedimage, but I would like to resize it with the parameters I see in the tutorial (ie icon.x = 50 icon.y = 300, fitImage( icon, 200, 200, true)) Thank you very much for your help in advance!

    local json = require( "json" )
    local function networkListener( event )
        local res = json.prettify( event.response )
        local decoded = json.decode( res )
        if ( event.isError ) then
            print( "--Network error-- ", ( res ) )
        else
            print( "Results: " .. ( res ) )
            --Here a JSON value (URL) is fetched from an API call and use it as a remote image 
            local edmPreview = decoded.items[lot20].edmPreview[1]

            -- Display image from web
            local function networkListener1image( event )
                if ( event.isError ) then
                    print ( "Network error - download failed" )
                else
                    event.target.alpha = 0
                    transition.to( event.target, { alpha = 1.0 } )
                end

                print ( "event.response.fullPath: ", event.response.fullPath )
                print ( "event.response.filename: ", event.response.filename )
                print ( "event.response.baseDirectory: ", event.response.baseDirectory )

            end
            local loadedimage = display.loadRemoteImage(edmPreview, "GET", networkListener1image, "image1.png", system.TemporaryDirectory, 200, display.contentCenterY )

        end
    end
    network.request("https://www.europeana.eu/api/v2/search.json?wskey="..apikey.europeana.."&query=art&qf=when:1900&start="..lotmax.."&reusability=open&media=true", "GET", networkListener, params)

 



[TOPIC: post.html]
#2

GrahamRanson

[GLOBAL: userInfoPane.html]
GrahamRanson
  • Contributor

  • 107 posts
  • Corona SDK

  Best Answer

Inside the networkListener function there is a table passed in called "event", on this table is a variable called "target" - this is your display object.


  • GO4IT likes this

[TOPIC: post.html]
#3

GO4IT

[GLOBAL: userInfoPane.html]
GO4IT
  • Observer

  • 15 posts
  • Corona SDK

Inside the networkListener function there is a table passed in called "event", on this table is a variable called "target" - this is your display object.

 

Thank you so much @GrahamRanson! It is quite straightforward to find a solution. I did not know Lua enough.

So, the solution was:

    --including a module
    local resize = require( "module_imageresize" )
    local json = require( "json" )
    local function networkListener( event )
        local res = json.prettify( event.response )
        local decoded = json.decode( res )
        if ( event.isError ) then
            print( "--Network error-- ", ( res ) )
        else
            print( "Results: " .. ( res ) )
            --Here a JSON value (URL) is fetched from an API call and use it as a remote image
            local edmPreview = decoded.items[lot20].edmPreview[1]

            -- Display image from web
            local function networkListener1image( event )
                if ( event.isError ) then
                    print ( "Network error - download failed" )
                else
                    event.target.alpha = 0
                    transition.to( event.target, { alpha = 1.0 } )
                end

                print ( "event.response.fullPath: ", event.response.fullPath )
                print ( "event.response.filename: ", event.response.filename )
                print ( "event.response.baseDirectory: ", event.response.baseDirectory )

                myImage = event.target
                resize.fitImage(myImage, 200, 200, true )

            end
            local loadedimage = display.loadRemoteImage(edmPreview, "GET", networkListener1image, "image1.png", system.TemporaryDirectory, 200, display.contentCenterY )

        end
    end
    network.request("https://www.europeana.eu/api/v2/search.json?wskey="..apikey.europeana.."&query=art&qf=when:1900&start="..lotmax.."&reusability=open&media=true", "GET", networkListener, params)

I am calling a Module (module_imageresize.lua) which is basically the function explained in the tutorial.

local M = {}
M.fitImage = function( displayObject, fitWidth, fitHeight, enlarge )
    -- first determine which edge is out of bounds
    local scaleFactor = fitHeight / displayObject.height
    local newWidth = displayObject.width * scaleFactor
    if newWidth > fitWidth then
        scaleFactor = fitWidth / displayObject.width
    end
    if not enlarge and scaleFactor > 1 then
        return
    end
    displayObject:scale( scaleFactor, scaleFactor )
end
return M
 

[TOPIC: post.html]
#4

GrahamRanson

[GLOBAL: userInfoPane.html]
GrahamRanson
  • Contributor

  • 107 posts
  • Corona SDK

Glad I could help!




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: resize, image, loadremoteimage, api