Quartzb2D

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();


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值