2

i have UIView which displays the Graph..now I need to capture it to the UIImage and I googled it and got the below code.but if i use this in my code its not work.even if I use breakpoint compiler does not reach to this.I am using this in my UIView .where I am going wrong?

+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return img;
} 
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Christien
  • 1,213
  • 4
  • 18
  • 32

3 Answers3

3

You can take a screenshot of any view or whole screen of the iPhone app with below method

- (UIImage *)captureView {

//hide controls if needed
CGRect rect = [self.view bounds];

    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:context];   
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;

}

And call it like bellow...

UIImage *tempImageSave=[self captureView];

and you can also save this image with this bellow line for photo album..

UIImageWriteToSavedPhotosAlbum(tempImageSave,nil,nil,nil);

and you can also save this image with this bellow line for Document Directory..

NSData *imageData = UIImagePNGRepresentation(tempImageSave);
NSFileManager *fileMan = [NSFileManager defaultManager];

NSString *fileName = [NSString stringWithFormat:@"%d.png",1];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
[fileMan createFileAtPath:pdfFileName contents:imageData attributes:nil];

If the view contains the layer images or some graphics related data then use below method.

-(UIImage *)convertViewToImage:(UIView *)viewTemp
{
    UIGraphicsBeginImageContext(viewTemp.bounds.size);
    [viewTemp drawViewHierarchyInRect:viewTemp.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;

}

And use this method like below.

UIImage *tempImageSave = [self convertViewToImage:yourView];

I hope this helpful for you.

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • need to use this code in UIView right?? but property View is not found error – Christien Dec 27 '12 at 05:40
  • hey dude see which you want to capture the view?? and what you want this image?? you want to save this image in device directory?? – Paras Joshi Dec 27 '12 at 05:42
  • @Christien just put above method in your .m file dude and then call it like my above code also add this captured image in UIImageView and check its display the proper photo which you captured dude.. :) – Paras Joshi Dec 27 '12 at 05:44
  • @Christien dude my last line save your captured view image in your directory , very simple – Paras Joshi Dec 27 '12 at 05:53
  • @ParasJoshi its save in to Device Photo Album not in Xcode simulator Document Directory :) – Nitin Gohel Dec 27 '12 at 05:56
  • @NitinGohel ya ya dude Photo Album dude... and also in simulatore its save this image just try you bro.. try kar ne yaar.. ;) – Paras Joshi Dec 27 '12 at 05:59
  • kari atle j to kahyu bhai ke document directory manathi avtu Simulatore photo album ma ave che document director mate filemanager thi savekaravvi pade ne dost ... :) – Nitin Gohel Dec 27 '12 at 06:00
  • oh ok ok mane thyu k tu m kahe 6 k aa simulator ma work j nti kartu means save j nti kartu m ok ok .. but tu kya re 6 and kai company ma 6?? – Paras Joshi Dec 27 '12 at 06:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21741/discussion-between-nitin-gohel-and-paras-joshi) – Nitin Gohel Dec 27 '12 at 06:05
1

you can save image of View like this way and store image in to Document Directory:-

-(void)SaveViewAsImage
{
    UIGraphicsBeginImageContext(self.view.bounds.size);

        UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIGraphicsEndImageContext();
        NSData *imageData = UIImagePNGRepresentation(saveImage);
        NSFileManager *fileMan = [NSFileManager defaultManager];

        NSString *fileName = [NSString stringWithFormat:@"%d.png",1];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
        [fileMan createFileAtPath:pdfFileName contents:imageData attributes:nil];

}

Your image save in Document Directory :)

Download this demo

http://www.sendspace.com/file/gjxa82

Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
1

Try with this code it may be help you

- (UIImage *)captureView:(UIView *)view {
    CGRect screenRect = [[UIScreen mainScreen] bounds];

    UIGraphicsBeginImageContext(screenRect.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor clearColor] set];
    CGContextFillRect(ctx, screenRect);

    [view.layer renderInContext:ctx];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;
}
Hiren
  • 12,720
  • 7
  • 52
  • 72