//
//  SnapShot.m
//
//  Created by AIG-Soft on 2011/11/16.
//  Copyright (c) 2011 AIG-Soft. All rights reserved.
//

#import "SnapShot.h"
#import <QuartzCore/CoreAnimation.h>	// QuartzCore.frameworkが必要

// context = 描画されている領域
// viewは描画対象で、contextの大きさを持つ要素
void snapShot1(CGContextRef context,UIView *view)
{
	printFunc();
	
	// -renderInContext: renders in the coordinate space of the layer, so we must first apply the layer's geometry to the graphics context
	CGContextSaveGState(context);

	// Center the context around the window's anchor point
	CGRect  frame =view.frame;
//	nslog(@"frame.origin=%f,%f",frame.origin.x,frame.origin.y);
	CGPoint center=view.center;
//	nslog(@"center=%f,%f",center.x,center.y);
	CGContextTranslateCTM(context, center.x-frame.origin.x, center.y-frame.origin.y); // centerではframeの画面上でのオフセットが反映されないので
	
	// Apply the window's transform about the anchor point
	CGContextConcatCTM(context, view.transform);

	// Offset by the portion of the bounds left of and above the anchor point
	CGSize size=view.bounds.size;
	CALayer *layer=view.layer;
//	nslog(@"layer=%@",layer);
	CGPoint anchorPoint=layer.anchorPoint;
//	nslog(@"anchorPoint=%f,%f",anchorPoint.x,anchorPoint.y);
	CGContextTranslateCTM(context, -size.width *anchorPoint.x, -size.height*anchorPoint.y);
	
	// Render the layer hierarchy to the current context
//	nslog(@"renderInContext");
	[layer renderInContext:context];
	
	// Restore the context
//	nslog(@"CGContextRestoreGState");
	CGContextRestoreGState(context);
	//
	nslog(@"snapShot1 end");
}
