2

I m new to iPhone developing. not much familiar about view's and layouts in iPhone Technology.

I want to achieve is :

  • I am in page that contains one button, on click of that button i open chart view (using core plot).
  • I want to take a screen shot of that chart view but i dont want to open that view.

Is this possible ?

Any help will be appreciated.

IronManGill
  • 7,222
  • 2
  • 31
  • 52

4 Answers4

5

Yes, you can composite the layer of the view onto a bitmap context and then obtain an image object from it:

CGContextRef CGContextCreate(CGSize size)
{
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
    CGContextRef ctx = CGBitmapContextCreate(NULL, size.width, size.height, 8, size.width * (CGColorSpaceGetNumberOfComponents(space) + 1), space, kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease(space);
    return ctx;
}

- (UIImage *)screenshotView:(UIView *)view
{
    CGSize size = view.frame.size;

    // Check for retina display
    if ([[UIScreen mainScreen] scale] > 1.5f) {
        size.width *= 2;
        size.height *= 2;
    }

    CGContextRef ctx = CGContextCreate(size);
    CGAffineTransform normalize = CGAffineTransformMake(1, 0, 0, -1, 0, size.height);
    CGContextConcatCTM(ctx, normalize);

    [[view layer] renderInContext:ctx];

    CGImage cgImg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgImg];
    CGImageRelease(cgImg);
    CGContextRelease(ctx);

    return img;
}
  • 2
    @xDeveloper ***You don't have to show that view. Just create it and pass it to the method I provided.*** Sigh... –  Nov 10 '12 at 09:04
  • I have view Controller not view –  Nov 10 '12 at 09:24
  • 1
    @xDeveloper A view controller controls a view, hence it's name. So you do have a view and you have its controller. If you don't have a reference to the view itself, the controller holds one. – JustSid Nov 10 '12 at 09:28
  • it shows me only background image & the chart view is not shown –  Nov 10 '12 at 09:32
  • @xDeveloper Try passing in `[[viewController.view containerLayer] presentationLayer]` instead, does that solve it? –  Nov 10 '12 at 09:33
  • app crashed because containerLayer method is not found –  Nov 10 '12 at 09:40
  • In that view I have custom chart view say hostview. But also try by passing VC.hostview but also shown background image. –  Nov 10 '12 at 09:43
  • 1
    @xDeveloper `[viewController.view presentationLayer]`, perhaps? (Please, don't be unviable! You should be able to fix a crash...) –  Nov 10 '12 at 09:46
  • presentationLayer method is also not avaiLable. I don't know what's the problem?? In the debug section says like: "[UIView containerLayer] unrecognized selector send to instance –  Nov 10 '12 at 09:57
1

Yes, you can do this.

UIView *yourView = /* get your chart view */;
UIGraphicsBeginImageContext(yourView.bounds.size);
[yourView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *yourViewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Now you have a UIImage with the image of your view.

Cezary Wojcik
  • 21,745
  • 6
  • 36
  • 36
0
UIGraphicsBeginImageContext(self.view.bounds.size); // You can put your view here.
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data = [UIImagePNGRepresentation(image) retain];
UIImage *screenShot = [UIImage imageWithData:data];
Rushi
  • 4,553
  • 4
  • 33
  • 46
0

This is built into Core Plot:

UIImage *screenShot = [graph imageOfLayer];

Where graph is your Core Plot graph (CPTXYGraph), not the hosting view. Be sure to set the size of the graph before calling -imageOfLayer if it's not in a hosting view that's already visible somewhere on screen. You don't need a hosting view at all if you don't intend to display it on screen.

Eric Skroch
  • 27,381
  • 3
  • 29
  • 36