Showing posts with label save. Show all posts
Showing posts with label save. Show all posts

Wednesday, 24 July 2013

How to capture whole screen in Corona SDK

You can use the following lines of code to capture the device screen in Corona SDK. This will save the screenshot as: Picture 1.png, Picture 2.png, etc in the sandbox, and will show it on the screen with half size(if you do not need this, you can remove it by calling captured_image:removeSelf() and captured_image=nil). It can save upto 10000 files.

Syntax:

    local function captureDeviceScreen()
       local captured_image = display.captureScreen( true )
       captured_image:scale(.5,.5)
       local alert = native.showAlert( "Success", "Captured Image is Saved to Library", { "OK" } )
    end
    Runtime:addEventListener("tap",captureDeviceScreen)


And if you want to save the image with desired name, then you can refer the following post:

How to mail a screen captured image using corona SDK



How to mail a screen captured image using corona SDK

For doing this, you have to:
  • First create a localgroup.
  • Then add the screen objects to that group.
  • Return the display group
  • Use display.save to save the entire group displayed.
  • Create mail option and add attachment image from baseDirectory
  • Call mail Popup
Example

-- creating the display group --
local localGroup = display.newGroup()  

-- creating display objects and adding it to the group --
local bg = display.newRect(0,0,320,480)
bg.x = 160
bg.y = 240
bg:setFillColor(150)
localGroup:insert(bg)

local rect = display.newRect(0,0,50,50)
rect.x = 30+math.random(260)
rect.y = 30+math.random(420)
localGroup:insert(rect)

-- Then do as follows --
local function takePhotoAndSendMail()
  -- take screen shot to baseDirectory --
  local baseDir = system.DocumentsDirectory
  display.save( localGroup, "myScreenshot.jpg", baseDir )

  -- Create mail options --
  local options =
  {
    to = { "devblogdk@gmail.com",},
    subject = "My Level",
    body = "Add this...",
    attachment =
    {
      { baseDir=system.DocumentsDirectory, filename="myScreenshot.jpg", 
        type="image" },
    },
  }

  -- Send mail --
  native.showPopup("mail", options)
end
rect:addEventListener("tap",takePhotoAndSendMail)

Sunday, 2 June 2013

Using camera to take picture and save that to file: cocos2d

/* Using camera to take a photo */

    // in .h File //
        @interface HelloWorld : CCLayer <UIImagePickerControllerDelegate, UINavigationControllerDelegate>{
   
            UIImagePickerController *uip;
            UIImage *profileImage;
        }

    // in .m File //
        #import "AppDelegate.h"

        -(void)takePhoto{
            AppDelegate *appdel = [UIApplication sharedApplication].delegate;
   
            @try {
       
                uip = [[UIImagePickerController alloc] init] ;
                uip.sourceType = UIImagePickerControllerSourceTypeCamera;
                uip.allowsEditing = YES;
                uip.delegate = self;
            }
            @catch (NSException * e) {
                [uip release];
                uip = nil;
            }
            @finally {
                if(uip) {
                    [appdel.viewController presentModalViewController:uip animated:YES];
                }
            }
        }



    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
   
        profileImage=[info objectForKey:UIImagePickerControllerEditedImage];
        AppDelegate *appdel = [UIApplication sharedApplication].delegate;
        [appdel.viewController dismissModalViewControllerAnimated:YES];
        [uip release];
       
        [NSThread detachNewThreadSelector:@selector(writeImgToPath:) toTarget:self withObject:profileImage]; 
         
       
    }


// To write the image to file //
    -(void)writeImgToPath:(id)sender
    {
       
        NSAutoreleasePool *pool = [NSAutoreleasePool new];
        UIImage *image = sender;
        NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                           NSUserDomainMask,
                                                           YES);
        CGSize size;
        int currentProfileIndex = 1
        NSString *path = [[pathArr objectAtIndex:0]
                      stringByAppendingPathComponent:[NSString stringWithFormat:@"Img_%d.png",currentProfileIndex]];           
       
        size = CGSizeMake(120, 120);
        UIGraphicsBeginImageContext(size);
        [image drawInRect:CGRectMake(0, 0, 120, 120)];
        image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
   
        NSData *data = UIImagePNGRepresentation(image);
        [data writeToFile:path atomically:YES];
        NSLog(@"Saved.....");
       
        CGRect r = CGRectMake(0, 0, 80, 80);
        UIGraphicsBeginImageContext(r.size);
   
        UIImage *img1;
   
        [image drawInRect:r];
        img1 = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
       
        [pool release];
    }