Advertisement
Guest User

Untitled

a guest
Mar 16th, 2015
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.02 KB | None | 0 0
  1. local M = {}
  2.  
  3.  
  4. function M.save(photo, newFileName, width, height)
  5.    
  6.     -- End size we want, defaults are profile image size
  7.     local endWidth  = 256 * display.contentScaleX
  8.     local endHeight = 256 * display.contentScaleY
  9.     if width then endWidth = width * display.contentScaleX end
  10.     if height then endHeight = height * display.contentScaleY end
  11.    
  12.     local tempGroup = display.newGroup()
  13.    
  14.     photo.anchorX   = 0.5
  15.     photo.anchorY   = 0.5
  16.     photo.x         = display.contentCenterX
  17.     photo.y         = display.contentCenterY
  18.     tempGroup:insert(photo)
  19.    
  20.     -- Find the bigger scale out of widht or height so it will fill in the crop
  21.     local scale = math.max(endWidth / photo.contentWidth, endHeight / photo.contentHeight)
  22.     photo.width = photo.width * scale
  23.     photo.height = photo.height * scale
  24.    
  25.     -- This object will be used as screen capture boundaries object
  26.     local cropArea   = display.newRect(display.contentCenterX, display.contentCenterY, endWidth, endHeight)
  27.     cropArea.anchorX = 0.5
  28.     cropArea.anchorY = 0.5
  29.     cropArea.x       = display.contentCenterX
  30.     cropArea.y       = display.contentCenterY
  31.     cropArea.alpha   = 0
  32.     tempGroup:insert(cropArea)
  33.    
  34.     -- Now capture the crop area which the user image will be underneith
  35.     local myCaptureImage = display.captureBounds(cropArea.contentBounds)
  36.  
  37.     myCaptureImage.anchorX  = 0.5
  38.     myCaptureImage.anchorY  = 0.5
  39.     myCaptureImage.x        = display.contentCenterX
  40.     myCaptureImage.y        = display.contentCenterY
  41.  
  42.     display.save(myCaptureImage, {filename=newFileName, baseDir=system.DocumentsDirectory, isFullResolution=true, backgroundColor={0, 0, 0, 0}})
  43.     local syncResults, syncErrStr = native.setSync(newFileName, {iCloudBackup = false}) -- Required or app gets declined for iOS
  44.     myCaptureImage:removeSelf() -- Remove captured image since we have the image from camera already visible
  45.     myCaptureImage = nil
  46.  
  47.     tempGroup:removeSelf()
  48.     tempGroup = nil
  49.  
  50. end
  51.  
  52.  
  53.  
  54. return M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement