I got something that seems to work… unfortunatley display.save applies scaling, so on small devices the cropped image is correct (in terms of chat was cropped) but is smaller than the crop dimensions… and on larger devices the image is bigger than the crop dimensions.
Is there a way of turning off corona’s automatic scaling - just for one action?
This is what I’ve got.
function U.cropImageOnDevice(filename, newFileName, cropX, cropY, cropW, cropH) local photo=display.newImage( filename, system.TemporaryDirectory ) local endWidth = cropW local endHeight = cropH -- set the masking container local tempGroup = display.newSnapshot(endWidth, endHeight ) tempGroup.x = 0 tempGroup.y = 0 tempGroup.anchorX = 0 tempGroup.anchorY = 0 -- Define a solid color background, in case the final image is -- smaller than the cropping output local whiteRc = display.newRect( 0, 0, endWidth, endHeight ) tempGroup.group:insert(whiteRc) whiteRc.anchorX = 0.5 whiteRc.anchorY = 0.5 whiteRc:setFillColor( 1,1,1 ) -- insert the photo tempGroup.group:insert(photo) -- set photo to x0 to start the crop at x=0 local x0 = cropW \* -0.5 local y0 = cropH \* -0.5 local x = x0 + ( cropX \* -1 ) local y = y0 + ( cropY \* -1 ) photo.x = x photo.y = y photo.anchorX = 0 photo.anchorY = 0 -- save the cropped image display.save( tempGroup, { filename = newFileName, baseDir = system.TemporaryDirectory, isFullResolution = true } ) -- clean up the mess tempGroup:removeSelf() tempGroup = nil end
U.cropImageOnDevice(“a.jpg”, “b.jpg”, 100, 400, 500, 500)