0

I am taking the screenshot of my last viewed Screen, by the following code

//Image Code  
UIImage *nextImage;  
CGImageRef imgRef = CGBitmapContextCreateImage(UIGraphicsGetCurrentContext());  
nextImage = [UIImage imageWithCGImage:imgRef];  
CGImageRelease(imgRef);  
CGContextRelease(UIGraphicsGetCurrentContext());   

When i displayed the the image in the console i got the O/P like: UIImage: 0x929c760
The Problem lies here for me, When converting that UIImage to NSData i am getting null.

The things i have tried are

//tried    
NSData *data = UIImagePNGRepresentation(nextImage);  
NSData *data2 = UIImageJPEGRepresentation(nextImage, 1.0);
Jonas Schnelli
  • 9,965
  • 3
  • 48
  • 60
iYahoo
  • 184
  • 3
  • 16
  • I was converting CGContext to UIImage. Now that UIImage is to be converted into NSData – iYahoo Apr 19 '12 at 12:46
  • 1
    You are not populating your graphics context with any image - so it is empty http://stackoverflow.com/questions/2200736/how-to-take-a-screenshot-programmatically – NP Compete Apr 19 '12 at 14:14
  • Yeah i had just seen that and really worked for me – iYahoo Apr 20 '12 at 05:03

1 Answers1

2

Hi everyone i have got the solution to my Question as given by the link by @NP Compete //getting screenshot i haven't used any UIImageview so my code goes as following

-(UIImage*)doImageOperation
{
    UIImage *image = [[UIImage alloc]init];
    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSLog(@"image in cg %@",image);
    [self saveImage:image];
    return image;
}

// Saved into Documents Directory

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            path = [documentsDirectory stringByAppendingPathComponent: 
                    [NSString stringWithString:@"test.png"]];
 data = UIImagePNGRepresentation(image);
        [data writeToFile:path atomically:YES];
NSData *data = UIImagePNGRepresentation(image1);
        NSLog(@"data %@",data);
iYahoo
  • 184
  • 3
  • 16