现在的位置: 首页 > 综合 > 正文

Quartzb2D

2013年09月20日 ⁄ 综合 ⁄ 共 4721字 ⁄ 字号 评论关闭

Quartz转换实现的原理,Quartz把绘图分成两个部分,

用户空间,即和设备无关,

设备空间;

用户空间和设备空间中间存在一个转换矩阵:CTM

Quartz提供三大功能

移动、旋转、缩放

演示如下,首先加载一张图片

演示如下,首先加载一张图片

void CGContextDrawImage (    CGContextRef c,    CGRect rect,    CGImageRef image );

移动函数
CGContextTranslateCTM (myContext, 100, 50);


旋转函数
include <math.h> static inline double radians (double degrees) {return degrees * M_PI/180;}
CGContextRotateCTM (myContext, radians(–45.));


缩放
CGContextScaleCTM (myContext, .5, .75);

翻转, 两种转换合成后的效果,先把图片移动到右上角,然后旋转180度

CGContextTranslateCTM (myContext, w,h); CGContextRotateCTM (myContext, radians(-180.));


//***************************************************************************************************************************************************//

组合几个动作

CGContextTranslateCTM (myContext, w/4, 0); 
CGContextScaleCTM (myContext, .25,  .5);
CGContextRotateCTM (myContext, radians ( 22.));

//=======================================================================================================================

CGContextRotateCTM (myContext, radians ( 22.)); 
CGContextScaleCTM (myContext, .25,  .5);
CGContextTranslateCTM (myContext, w/4, 0);


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

上面是通过直接修改当前的ctm实现3大效果,下面是通过创建Affine Transforms,然后连接ctm实现同样的3种效果
这样做的好处是可以重用这个Affine Transforms
应用Affine Transforms 到ctm的函数
void CGContextConcatCTM (CGContextRef c,    CGAffineTransform transform );


Creating Affine Transforms
移动效果
CGAffineTransform CGAffineTransformMakeTranslation (CGFloat tx, CGFloat ty );
CGAffineTransform CGAffineTransformTranslate (CGAffineTransform t, CGFloat tx, CGFloat ty );
旋转效果
CGAffineTransform CGAffineTransformMakeRotation (CGFloat angle );
CGAffineTransform CGAffineTransformRotate (CGAffineTransform t, CGFloat angle );
缩放效果
CGAffineTransform CGAffineTransformMakeScale ( CGFloat sx, CGFloat sy );
CGAffineTransform CGAffineTransformScale (CGAffineTransform t, CGFloat sx, CGFloat sy );
反转效果
CGAffineTransform CGAffineTransformInvert (CGAffineTransform t );
只对局部产生效果
CGRect CGRectApplyAffineTransform (CGRect rect, CGAffineTransform t );
判断两个AffineTrans是否相等
bool CGAffineTransformEqualToTransform (CGAffineTransform t1, CGAffineTransform t2 );
获得Affine Transform
CGAffineTransform CGContextGetUserSpaceToDeviceSpaceTransform ( CGContextRef c );
下面的函数只起到查看的效果,比如看一下这个用户空间的点,转换到设备空间去坐标是多少
CGPoint CGContextConvertPointToDeviceSpace (CGContextRef c, CGPoint point );
CGPoint CGContextConvertPointToUserSpace (CGContextRef c, CGPoint point );
CGSize CGContextConvertSizeToDeviceSpace (CGContextRef c, CGSize size );
CGSize CGContextConvertSizeToUserSpace (CGContextRef c, CGSize size );

CGRect CGContextConvertRectToDeviceSpace ( CGContextRef c, CGRect rect );
CGRect CGContextConvertRectToUserSpace ( CGContextRef c, CGRect rect );

//*************************************************************************************************************************************************************************//
- (void)initPDFImage{

CGContextRef   context = CGBitmapContextCreate(NULLpageSize.width, pageSize.height,
 8,/* bits per component*/ pageSize.width *4/* bytes
per row */  
colorSpace,  kCGImageAlphaPremultipliedLast|kCGBitmapByteOrder32Big);       //创建上下文

CGAffineTransform  scale = CGAffineTransformMakeScale(scaleFactor, scaleFactor);

CGRect scaledInnerRect = CGRectApplyAffineTransform(innerRect, scale);

CGAffineTransformtranslation = CGAffineTransformMakeTranslation((outerRect.size.width -
scaledInnerRect.size.width) / 2 - scaledInnerRect.origin.x

(outerRect.size.height - scaledInnerRect.size.height)
2 - scaledInnerRect.origin.y);

CGAffineTransformtransform = CGAffineTransformConcat(scale, translation);  //连接两个
CGAffineTransform


CGContextConcatCTM(context,transform);

CGContextDrawPDFPage(context,_pageRef);

CGImageRef image =CGBitmapContextCreateImage(context);

CGContextRelease(context);

}
//==========================================================================================//
- (void)createImage{

CGPDFPageRef page = CGPDFDocumentGetPage(cgPdfRef,number+1);

UIGraphicsBeginImageContext(CGSizeMake(156208));      //创建上下文

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetRGBFillColor( context, 1.01.01.01.0 );

CGContextFillRect( context, CGContextGetClipBoundingBox(context));

CGContextTranslateCTM(context, 0.0208);

CGContextScaleCTM(context, 1.0, -1.0);

CGContextSaveGState(context);

CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox,CGRectMake(0.0f0.0f156.0f208.0f), 0true);

CGContextConcatCTM(context, pdfTransform);

CGContextDrawPDFPage(context, page);

CGContextRestoreGState(context);

UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();


抱歉!评论已关闭.