display.save() causing image to disappear

I have an app which allows the user to take a picture from either the camera or gallery. 

The app then saves this image to the TemporaryDirectory, rescales it to 0.25, saves a thumbnail, and then rescales back to 1.

It worked fine before, but recently we have noticed that the images kept being displayed as black squares, sometimes with text in them. If I look in the sandbox then the images in the temp directory appear to be fine though.

I’ve made a quick sample, and I’ve found that when display.save is called, the image disappears entirely:

local \_W, \_H = display.contentWidth, display.contentHeight local isSimulator = "simulator" == system.getInfo("environment") local photo local function onCamera(e) local onComplete = function(event) if event.target then --remove photo if it already exists if photo then photo:removeSelf() photo = nil end local photogroup = display.newGroup() photo = event.target photo.x, photo.y = \_W \* 0.5, \_W \* 0.5 photogroup:insert(photo) --save image --THIS CAUSES IMAGE TO DISAPPEAR local baseDir = system.TemporaryDirectory display.save(photogroup, "myPhoto.jpg", baseDir) end end local function onChoiceComplete(event) if event.index == 1 then if not isSimulator then local filePath = { baseDir = system.TemporaryDirectory, filename = "CameraShot.jpg" } media.show( media.Camera, onComplete ) else media.show( media.Camera, onComplete) end elseif event.index == 2 then if media.hasSource( media.PhotoLibrary ) then if not isSimulator then local filePath = { baseDir = system.TemporaryDirectory, filename = "CameraShot.jpg" } media.show( media.PhotoLibrary, onComplete ) else media.show( media.PhotoLibrary, onComplete) end end end end if e.phase == "ended" then local alert = native.showAlert( "Photo", "Choose photo from:", { "Camera","Gallery"},onChoiceComplete ) end end local background = display.newRect(\_W \* 0.5, \_H \* 0.5, \_W, \_H) background:setFillColor( 1, 0, 0 ) local cameraButton = display.newRoundedRect(\_W \* 0.5, 50, 50, 50, 10) cameraButton:addEventListener( "touch", onCamera ) cameraButton.strokeWidth = 2 cameraButton:setStrokeColor( 0 ) local cameraText = display.newText( "Camera", cameraButton.x, cameraButton.y, native.systemFont, 12 ) cameraText:setFillColor( 0 )

Has anyone noticed the same thing happening to them?

I’ve thinned out my sample to remove the scaling beforehand, but the problem still persists. I’ve also tried using:

display.save( photogroup, { filename="myPhoto.jpg", baseDir=system.TemporaryDirectory, isFullResolution=true } )

which did exactly the same thing.

Had the same problem. This is how I solved it.

  1. Instead of using ‘media.show’ which has been deprecated, use this : http://docs.coronalabs.com/api/library/media/selectPhoto.html

  2. After doing all your processing, use the ‘:toFront()’ function to  bringg your image back to the front.

I am using Corona 2013.2109 in v1 compatibility mode.

Didn’t work for me, I’m not using v1 compatibility mode. Using toFront() also made no difference, it’s as if the image has completely disappeared.

When I run my sample code, the text on the cameraText object also changes colour in the OS X simulator when the onComplete function is called, no idea why that happens.

Is anyone from Corona able to run my code snippet and see if they see the same error?

A quick observation - around line 20, you’re setting the y coord of the image to 1/2 the width – not height, which it should be I think.

If the image (or group) is offscreen – nothing will show up of course…

You’re quite right, given the screen ratio though, the image should still appear on screen. I’ve just tried it again using build 2122, and I’m still just getting a black square instead of my image.

As mentioned in the code’s comments this line:

display.save(photogroup, "myPhoto.jpg", baseDir)

is responsible for the problem.

If I comment it out then the image remains as it should be, if I put it back in then the image becomes a black square. The image in the sandbox folder is correct, so I’m not sure if it’s being encoded incorrectly when it saves or something like that, which is preventing it from being reopened correctly?

Is anyone able to confirm that they see the same behaviour if they run my code?

ok, I just ran it on a latest gen ipod retina, and no black screen… (using build 2090, haven’t updated sim in a bit)

After the first photo, red screen, and button are there (visible, everything looks fine).

After taking a second photo, I get a photo put in front of everything - but, I can still click the button and take another…

That’s what I get with the code anyways… (Now, back to the profile issues I uncovered while building this test app…)

Did you try run your code using another image as the source?

UPDATE: Never mind, I tested here and it is indeed disappearing.

Try using using a timer before saving:

timer.performWithDelay(300,function() display.save(photogroup, "myPhoto.jpg", baseDir) end )

Yep, that seems to have fixed it. I had actually tried a timer before, but perhaps I put too short a delay in?  

Set it to 200ms and all is well again. 

Still not sure why this is happening though?

Had the same problem. This is how I solved it.

  1. Instead of using ‘media.show’ which has been deprecated, use this : http://docs.coronalabs.com/api/library/media/selectPhoto.html

  2. After doing all your processing, use the ‘:toFront()’ function to  bringg your image back to the front.

I am using Corona 2013.2109 in v1 compatibility mode.

Didn’t work for me, I’m not using v1 compatibility mode. Using toFront() also made no difference, it’s as if the image has completely disappeared.

When I run my sample code, the text on the cameraText object also changes colour in the OS X simulator when the onComplete function is called, no idea why that happens.

Is anyone from Corona able to run my code snippet and see if they see the same error?

A quick observation - around line 20, you’re setting the y coord of the image to 1/2 the width – not height, which it should be I think.

If the image (or group) is offscreen – nothing will show up of course…

You’re quite right, given the screen ratio though, the image should still appear on screen. I’ve just tried it again using build 2122, and I’m still just getting a black square instead of my image.

As mentioned in the code’s comments this line:

display.save(photogroup, "myPhoto.jpg", baseDir)

is responsible for the problem.

If I comment it out then the image remains as it should be, if I put it back in then the image becomes a black square. The image in the sandbox folder is correct, so I’m not sure if it’s being encoded incorrectly when it saves or something like that, which is preventing it from being reopened correctly?

Is anyone able to confirm that they see the same behaviour if they run my code?

ok, I just ran it on a latest gen ipod retina, and no black screen… (using build 2090, haven’t updated sim in a bit)

After the first photo, red screen, and button are there (visible, everything looks fine).

After taking a second photo, I get a photo put in front of everything - but, I can still click the button and take another…

That’s what I get with the code anyways… (Now, back to the profile issues I uncovered while building this test app…)

Did you try run your code using another image as the source?

UPDATE: Never mind, I tested here and it is indeed disappearing.

Try using using a timer before saving:

timer.performWithDelay(300,function() display.save(photogroup, "myPhoto.jpg", baseDir) end )

Yep, that seems to have fixed it. I had actually tried a timer before, but perhaps I put too short a delay in?  

Set it to 200ms and all is well again. 

Still not sure why this is happening though?

Thanks … the delay was the trick … everything was fine for me till i was testing on Android and got the dreaded black image. The delay did the trick … hope 300 is enough on all the devices :slight_smile:

Cheers!