Is there any way of sharing images without saving?

Hello everyone,

Is it possible to share images on social site without saving them? I am using display.captureScreen() to have my content screen getting captured then I am using native.showPopup(“social”,options) for sharing purpose.

https://docs.coronalabs.com/plugin/CoronaProvider_native_popup_social/showPopup.html

But what I have observed from the above link is native.showPopup() can share images after saving or images that are already saved. Is there any other alternative to work on it.

Thanks to all for their participation in this above content.

Thanks,

Swanand Thakur.

You probably will have to save the file. Then you can delete it when done.

Rob

Hello Sir @Rob Miracle,

local function capture() r=display.captureScreen( ) display.save( r,{filename="ss.png",baseDir=system.DocumentsDirectory} ) print("captured") end capture() local function popup() native.showPopup( "social",{ message="Running",image={filename="ss.png",baseDir=system.DocumentsDirectory}}) print("popup") end popup()

The above is the code I am applying but what’s happening is only the message “Running” is getting send and not the file would you please see if its something wrong inside.

And another problem is after popup exactly at what stage I should use os.remove() to remove the above saved file.

Thanks,

Swanand Thakur

We are able to find the solution of half part it was just an implication mistake, but still wondering exactly when the file should be removed.

local function capture() r=display.captureScreen( ) display.save( r,{filename="ss.png",baseDir=system.DocumentsDirectory} ) print("captured") end capture() local function popup() native.showPopup( "social",{ message="Running",image={{filename="ss.png",baseDir=system.DocumentsDirectory}}}) print("popup") end popup()

on the first line of popup() fnc the changes have been made just adding some curly brackets solved problem.

Please see if anyone can help on that part :slight_smile:

Thanks,

Swanand Thakur

You should not remove it until you’re done with it. The social popup should have a listener function that gets called on completion. That might be a good point to remove the file.

Rob

Hello Sir @Rob Miracle,

This is my improved code what’s happening in our testing devices is:-

  1. Multiple popus are been displayed.

2)Its taking a heavy time to showpopup.

3)When I choose apps to send the file, different apps shows different dialog box which indicates towards the point that file is getting removed before its getting send.

c=display.newCircle( 100,100,40) d=display.newCircle( 300,100,40) local function capture() r=display.captureScreen( ) r.x=display.contentCenterX r.y=display.contentCenterY display.save( r,{filename="ss.png",baseDir=system.DocumentsDirectory} ) r:removeSelf( ) native.showPopup( "social",{message="running",image={{filename="ss.png",baseDir=system.DocumentsDirectory},onComplete=os.remove( system.pathForFile( "ss.png", system.DocumentsDirectory ) ) }}) print("captured") end function c.touch(self,event) capture() end c:addEventListener( "touch", c )

Thanks,

Swanand Thakur.

I think the first thing you need to try is to fix your touch handler. Touch generates multiple events (event.phase == “began”, event.phase == “ended”, if you move during the touch, you will get “moved” phases.) So you’re going to end up calling capture() at a minimum of two times. Either change the “touch” to “tap” or change the touch function to:

function c.touch(self, event)      if event.phase == "ended" then           capture()      end end

Next you’re not handling your onComplete correctly.  The onComplete wants an “address to a function”. You’re calling os.remove() and passing it’s return value (true, false) to onComplete. This runs os.remove immediately and the return value is not a function address, so onComplete has nothing to do.

Also you’re code style makes it very hard to understand what you’re doing. Writing complex anonymous functions inline is hard to understand.  I would change that to:

local function cleanup() os.remove( system.pathForFile( "ss.png", system.DocumentsDirectory ) ) end local function capture() r=display.captureScreen( ) r.x=display.contentCenterX r.y=display.contentCenterY display.save( r, { filename = "ss.png", baseDir = system.DocumentsDirectory } ) display.remove(r) r = nil native.showPopup( "social", { message="running", image={ {filename = "ss.png", baseDir = system.DocumentsDirectory }, onComplete = cleanup } }) print("captured") end

To summarize:

  • Code formatting is important.
  • Touch handlers generate multiple events.
  • When using onComplete or other listener type handlers, there is a big difference between:  onComplete = functionName  and onComplete = functionName(). The first passes the address of the function, the second runs the function first and takes the return value as the address to the function which is rarely want you want.

Rob

Hello Sir @Rob Miracle,

Thank you so much for your help, the above observations of your’s has solved my problem but I would like to learn more about that last point you have said in summarize section regarding the return value and address?? Would you like to explain that part to me more clearly or share any links for self learning,because I have not understood that part.

Thanks again,

Swanand Thakur.

Let me try a simple example:

local myCircle = display.newCircle( display.contentCenterX, 0, 50 ) local function deleteCircle()       display.remove( myCircle )       myCircle = nil       return true end transition.to( myCircle, { time=5000, y = display.contentCenterY, onComplete = deleteCircle() } ) -- vs  transition.to( myCircle, { time=5000, y = display.contentCenterY, onComplete = deleteCircle } )

This will create a circle at the top of the content area, move it to the center and in theory remove the circle when it’s done. However, if you look at the first transition.to, because I included the (), that means run the function first and whatever value the function returns (in this case it will return true) assign that to the onComplete listener.

The net result is the circle will be removed immediately since you said “run the function” and you are left with this transition.to statement:

transition.to( myCircle, { time=5000, y = display.contentCenterY, onComplete = true } )

In this case, true is not a function, so onComplete has nothing to do.  When you use the second form with out the (), it says assign the value of the function’s memory address (something like 0x9485BA23) to onComplete. Lua knows this is a function and will turn the transition.to into:

transition.to( myCircle, { time=5000, y = display.contentCenterY, onComplete = 0x9485BA23 } )

When the code runs. Now transition.to sees a function assigned to onComplete, it will move the circle and when done, it will call the deleteCircle() function.

So doing:

l

ocal someVar = someFunction 

assigns the memory address for someFunction to the variable where as

local someVar = someFunction()

runs someFunction first and takes whatever value is passed back using the “return” statement (or nil as the default).

Rob

Sir @ Rob Miracle,

Thanks for such detailed explanation . I have understood the point clearly.

Regards,

Swanand Thakur

You probably will have to save the file. Then you can delete it when done.

Rob

Hello Sir @Rob Miracle,

local function capture() r=display.captureScreen( ) display.save( r,{filename="ss.png",baseDir=system.DocumentsDirectory} ) print("captured") end capture() local function popup() native.showPopup( "social",{ message="Running",image={filename="ss.png",baseDir=system.DocumentsDirectory}}) print("popup") end popup()

The above is the code I am applying but what’s happening is only the message “Running” is getting send and not the file would you please see if its something wrong inside.

And another problem is after popup exactly at what stage I should use os.remove() to remove the above saved file.

Thanks,

Swanand Thakur

We are able to find the solution of half part it was just an implication mistake, but still wondering exactly when the file should be removed.

local function capture() r=display.captureScreen( ) display.save( r,{filename="ss.png",baseDir=system.DocumentsDirectory} ) print("captured") end capture() local function popup() native.showPopup( "social",{ message="Running",image={{filename="ss.png",baseDir=system.DocumentsDirectory}}}) print("popup") end popup()

on the first line of popup() fnc the changes have been made just adding some curly brackets solved problem.

Please see if anyone can help on that part :slight_smile:

Thanks,

Swanand Thakur

You should not remove it until you’re done with it. The social popup should have a listener function that gets called on completion. That might be a good point to remove the file.

Rob

Hello Sir @Rob Miracle,

This is my improved code what’s happening in our testing devices is:-

  1. Multiple popus are been displayed.

2)Its taking a heavy time to showpopup.

3)When I choose apps to send the file, different apps shows different dialog box which indicates towards the point that file is getting removed before its getting send.

c=display.newCircle( 100,100,40) d=display.newCircle( 300,100,40) local function capture() r=display.captureScreen( ) r.x=display.contentCenterX r.y=display.contentCenterY display.save( r,{filename="ss.png",baseDir=system.DocumentsDirectory} ) r:removeSelf( ) native.showPopup( "social",{message="running",image={{filename="ss.png",baseDir=system.DocumentsDirectory},onComplete=os.remove( system.pathForFile( "ss.png", system.DocumentsDirectory ) ) }}) print("captured") end function c.touch(self,event) capture() end c:addEventListener( "touch", c )

Thanks,

Swanand Thakur.

I think the first thing you need to try is to fix your touch handler. Touch generates multiple events (event.phase == “began”, event.phase == “ended”, if you move during the touch, you will get “moved” phases.) So you’re going to end up calling capture() at a minimum of two times. Either change the “touch” to “tap” or change the touch function to:

function c.touch(self, event)      if event.phase == "ended" then           capture()      end end

Next you’re not handling your onComplete correctly.  The onComplete wants an “address to a function”. You’re calling os.remove() and passing it’s return value (true, false) to onComplete. This runs os.remove immediately and the return value is not a function address, so onComplete has nothing to do.

Also you’re code style makes it very hard to understand what you’re doing. Writing complex anonymous functions inline is hard to understand.  I would change that to:

local function cleanup() os.remove( system.pathForFile( "ss.png", system.DocumentsDirectory ) ) end local function capture() r=display.captureScreen( ) r.x=display.contentCenterX r.y=display.contentCenterY display.save( r, { filename = "ss.png", baseDir = system.DocumentsDirectory } ) display.remove(r) r = nil native.showPopup( "social", { message="running", image={ {filename = "ss.png", baseDir = system.DocumentsDirectory }, onComplete = cleanup } }) print("captured") end

To summarize:

  • Code formatting is important.
  • Touch handlers generate multiple events.
  • When using onComplete or other listener type handlers, there is a big difference between:  onComplete = functionName  and onComplete = functionName(). The first passes the address of the function, the second runs the function first and takes the return value as the address to the function which is rarely want you want.

Rob

Hello Sir @Rob Miracle,

Thank you so much for your help, the above observations of your’s has solved my problem but I would like to learn more about that last point you have said in summarize section regarding the return value and address?? Would you like to explain that part to me more clearly or share any links for self learning,because I have not understood that part.

Thanks again,

Swanand Thakur.

Let me try a simple example:

local myCircle = display.newCircle( display.contentCenterX, 0, 50 ) local function deleteCircle()       display.remove( myCircle )       myCircle = nil       return true end transition.to( myCircle, { time=5000, y = display.contentCenterY, onComplete = deleteCircle() } ) -- vs  transition.to( myCircle, { time=5000, y = display.contentCenterY, onComplete = deleteCircle } )

This will create a circle at the top of the content area, move it to the center and in theory remove the circle when it’s done. However, if you look at the first transition.to, because I included the (), that means run the function first and whatever value the function returns (in this case it will return true) assign that to the onComplete listener.

The net result is the circle will be removed immediately since you said “run the function” and you are left with this transition.to statement:

transition.to( myCircle, { time=5000, y = display.contentCenterY, onComplete = true } )

In this case, true is not a function, so onComplete has nothing to do.  When you use the second form with out the (), it says assign the value of the function’s memory address (something like 0x9485BA23) to onComplete. Lua knows this is a function and will turn the transition.to into:

transition.to( myCircle, { time=5000, y = display.contentCenterY, onComplete = 0x9485BA23 } )

When the code runs. Now transition.to sees a function assigned to onComplete, it will move the circle and when done, it will call the deleteCircle() function.

So doing:

l

ocal someVar = someFunction 

assigns the memory address for someFunction to the variable where as

local someVar = someFunction()

runs someFunction first and takes whatever value is passed back using the “return” statement (or nil as the default).

Rob

Sir @ Rob Miracle,

Thanks for such detailed explanation . I have understood the point clearly.

Regards,

Swanand Thakur