1. iOS 坐標(biāo)系
NSView坐標(biāo)系 : 左手坐標(biāo)系 原點(diǎn)左上角
UIView坐標(biāo)系 : 左手坐標(biāo)系 原點(diǎn)左上角
Quartz(Core Graphics)坐標(biāo)系 : 右手坐標(biāo)系 原點(diǎn)左下角
CALayer:在Mac中CALayer使用的是右手坐標(biāo)系条篷,其原點(diǎn)在左下角炸渡;iOS中使用的左手坐標(biāo)系西设,其原點(diǎn)在左上角暑竟。
2. iOS scale
iphone屏幕是用點(diǎn)坐標(biāo)來描述的铁蹈,不同于像素卓鹿。
[UIScreen mainScreen].scale可以獲得scale房揭,scale代表Scale Factor,每個(gè)點(diǎn)內(nèi)像素挤安,retain屏一個(gè)坐標(biāo)點(diǎn)內(nèi)像素不等于1的。
具體參照 : http://blog.csdn.net/phunxm/article/details/42174937
3. CGContextRef上下文
我們拿到上下文才可以使用coreGraphics和quatz的api進(jìn)行繪圖丧鸯。
1.UIView中
- (void) drawRect: (CGRect) rect {
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(con, CGRectMake(0,0,100,100));
CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor);
CGContextFillPath(con);
}
2 UIGraphicsBeginImageContextWithOptions
void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);
縮放因子越大蛤铜,轉(zhuǎn)化出來的圖片越大,清晰度越高丛肢,
BOOL opaque:默認(rèn)為YES围肥,不透明,位圖中沒有繪制的區(qū)域會(huì)以黑色顯示蜂怎;NO代表透明穆刻,
位圖中沒有繪制的區(qū)域會(huì)以透明顯示;主要是用于繪圖時(shí)進(jìn)行性能優(yōu)化的開關(guān)杠步。
CGFloat scale:代表縮放,0代表系統(tǒng)會(huì)自動(dòng)設(shè)置根據(jù)當(dāng)前設(shè)備的屏幕因數(shù)設(shè)置縮放因數(shù)氢伟。
UIGraphicsBeginImageContextWithOptions(CGSizeMake(sz.width, sz.height), NO, 2);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(ctx, 250.0/255, 250.0/255, 210.0/255, 1.0);
CGContextFillRect(ctx,rec);
CGContextDrawImage(ctx,CGRectMake(0, 0, sz.width, sz.height), img.CGImage);
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
3 CGBitmapContextCreate
CGContextRef CGBitmapContextCreate (
void *data,
size_t width,
size_t height,
size_t bitsPerComponent,
size_t bytesPerRow,
CGColorSpaceRef colorspace,
CGBitmapInfo bitmapInfo
);
data 指向要渲染的繪制內(nèi)存的地址。這個(gè)內(nèi)存塊的大小至少是(bytesPerRow*height)個(gè)字節(jié)幽歼。使用時(shí)可填NULL或unsigned char類型的指針腐芍。
width bitmap的寬度,單位為像素
height bitmap的高度,單位為像素
bitsPerComponent 內(nèi)存中像素的每個(gè)組件的位數(shù).例如對(duì)于32位像素格式和RGB 顏色空間你應(yīng)該將這個(gè)值設(shè)為8。
bytesPerRow bitmap的每一行在內(nèi)存所占的比特?cái)?shù)试躏,一個(gè)像素一個(gè)byte猪勇。
colorspace bitmap上下文使用的顏色空間。
bitmapInfo 指定bitmap是否包含alpha通道颠蕴,像素中alpha通道的相對(duì)位置泣刹,像素組件是整形還是浮點(diǎn)型等信息的字符串。
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef contextRef = CGBitmapContextCreate(NULL, img.size.width, img.size.height, 8, img.size.width * 4, colorSpace ,kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault);
CGContextDrawImage(contextRef, CGRectMake(0, 0, img.size.width, img.size.height), img.CGImage);
CGImageRef imageReff = CGBitmapContextCreateImage(contextRef);
UIImage* imageDst = [UIImage imageWithCGImage:imageReff scale:2 orientation:UIImageOrientationUp];
CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpace);
NSData *imageData = UIImagePNGRepresentation(imageDst);
4.上下文變換
我們可以把上下文想象成一張畫布就像PS中的一樣犀被。
畫布在建立的時(shí)候就已經(jīng)確定大小椅您。
使用CTM轉(zhuǎn)換函數(shù)可以轉(zhuǎn)換畫布坐標(biāo)系.
組合
void CGContextConcatCTM(CGContextRef c, CGAffineTransform transform);
旋轉(zhuǎn)
void CGContextTranslateCTM(CGContextRef c, CGFloat tx, CGFloat ty);
縮放
void CGContextScaleCTM(CGContextRef c, CGFloat sx, CGFloat sy);
平移
void CGContextRotateCTM(CGContextRef c, CGFloat angle);
CGAffineTransform CGContextGetCTM(CGContextRef c);
在前面我們說了coreGraphics坐標(biāo)系和設(shè)備坐標(biāo)系是不同的,所以一般我們需要轉(zhuǎn)換坐標(biāo)系后在上面繪制寡键。
UIImage* img = self.originImageV.image;
CGSize sz = [img size];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(sz.width, sz.height), NO, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
//坐標(biāo)軸轉(zhuǎn)換
CGContextTranslateCTM(ctx, 0, sz.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextDrawImage(ctx,CGRectMake(0, 0, sz.width, sz.height), img.CGImage);
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGContextRelease(ctx);
tips: CTM只是坐標(biāo)軸轉(zhuǎn)換掀泳,而畫布大小是不變的,意思是在后續(xù)繪制時(shí)如果坐標(biāo)超過了畫布的大小西轩,是繪制不到畫布上的员舵,列入下圖,當(dāng)坐標(biāo)軸旋轉(zhuǎn)了45度后在繪制圖片藕畔,那么實(shí)際上在畫布上的只有咖啡色的區(qū)域马僻,而其他部分都是上下文的背景色。
Quartz也提供了一組API去轉(zhuǎn)換坐標(biāo)注服。
CGAffineTransform transform = CGContextGetUserSpaceToDeviceSpaceTransform(context);
CGPoint devicePoint = CGContextConvertPointToDeviceSpace(context, CGPointMake(0, 0));
CGPoint userPoint = CGContextConvertPointToUserSpace(context, CGPointMake(0, 0));
CGSize deviceSize = CGContextConvertSizeToDeviceSpace(context, CGSizeMake(100, 100));
CGSize userSize = CGContextConvertSizeToUserSpace(context, CGSizeMake(100, 100));
CGRect deviceRect = CGContextConvertRectToDeviceSpace(context, CGRectMake(100, 100, 100, 100));
CGRect userRect = CGContextConvertRectToUserSpace(context, CGRectMake(100, 100, 100, 100));
值得注意的是韭邓,這一組函數(shù)不僅可以轉(zhuǎn)換坐標(biāo)措近,和上下文的scale也是相關(guān)聯(lián)的。
修正系數(shù)s = 設(shè)備.scale/上下文.scale ;
轉(zhuǎn)UserSpace會(huì)乘以修正系數(shù)s女淑。
轉(zhuǎn)DeviceSpac會(huì)除以修正系數(shù)s瞭郑。
上下文.scale是在創(chuàng)建上下文時(shí)候輸入的,而在UIView的drawRect方法中獲得的上下文的scale和設(shè)備的保持一致鸭你。
4. 裁剪圖片
非零纏繞規(guī)則 CGContextClip
如果邊界是順時(shí)針繪制凰浮,那么在其內(nèi)部逆時(shí)針繪制的邊界所包含的內(nèi)容為空。如果邊界是逆時(shí)針繪制苇本,那么在其內(nèi)部順時(shí)針繪制的邊界所包含的內(nèi)容為空袜茧。
奇偶規(guī)則 CGContextEOClip
最外層的邊界代表內(nèi)部都有效,都要填充瓣窄;之后向內(nèi)第二個(gè)邊界代表它的內(nèi)部無效笛厦,不需填充;如此規(guī)則繼續(xù)向內(nèi)尋找邊界線俺夕。我們的情況非常簡(jiǎn)單裳凸,所以使用奇偶規(guī)則就很容易了。這里我們使用CGContextEOCllip設(shè)置裁剪區(qū)域然后進(jìn)行繪圖劝贸。(從點(diǎn)引出射線如果和邊界交點(diǎn)數(shù)量為偶數(shù)姨谷,認(rèn)為在外部,奇數(shù)映九,則在內(nèi)部)
參考 : http://www.cnblogs.com/luckychen/p/5525794.html
裁剪區(qū)域簡(jiǎn)單一般采用 奇偶規(guī)則 CGContextEOClip
void CGContextClip(CGContextRef c);
void CGContextEOClip(CGContextRef c);
void CGContextClipToRects(CGContextRef c, const CGRect *rects, size_t count);
void CGContextClipToRect(CGContextRef c, CGRect rect);
void CGContextClipToRects(CGContextRef c, const CGRect *rects, size_t count);
void CGContextClipToMask(CGContextRef c, CGRect rect, CGImageRef mask);
CGContextMoveToPoint(con, 0, 0);
CGContextAddLineToPoint(con, 0, 200);
CGContextAddLineToPoint(con, 200, 200);
CGContextAddLineToPoint(con, 200, 0);
CGContextAddLineToPoint(con, 0, 0);
CGContextEOClip(con);// 奇偶規(guī)則
裁剪的意義是梦湘,當(dāng)裁剪完上下文后,圖像只能繪制在被裁剪的區(qū)域內(nèi)件甥,所以應(yīng)該是先裁剪捌议,后繪圖。
如果想要恢復(fù)裁剪前的可繪制上下可以用下面兩個(gè)方法引有,CGContextSaveGState在裁剪前瓣颅,CGContextRestoreGState在裁剪后。
CGContextSaveGState(con);
CGContextRestoreGState(con);
tips:所謂裁剪是裁剪上下文畫布的可繪制區(qū)域譬正,所以一定是先裁剪后繪制宫补。
可以使用UIBezierPath創(chuàng)建一個(gè)更為復(fù)雜的裁剪區(qū)域
UIImage* imageSrc = [UIImage imageNamed:@"island.png"];
CGColorSpaceRef colorRef = CGColorSpaceCreateDeviceRGB();
CGContextRef contextRef = CGBitmapContextCreate(nil, imageSrc.size.width, imageSrc.size.height, 8, imageSrc.size.width*4, colorRef, kCGImageAlphaPremultipliedFirst);
UIBezierPath* path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(30, 160)];
[path addQuadCurveToPoint:CGPointMake(140, 100) controlPoint:CGPointMake(80, 120)];
[path addQuadCurveToPoint:CGPointMake(240, 180) controlPoint:CGPointMake(180, 100)];
[path addQuadCurveToPoint:CGPointMake(140, 280) controlPoint:CGPointMake(210, 240)];
[path addQuadCurveToPoint:CGPointMake(30, 160) controlPoint:CGPointMake(80, 260)];
[path closePath];
[path addClip]; //在當(dāng)前上下文環(huán)境中.
CGContextDrawImage(contextRef, CGRectMake(0, 0, imageSrc.size.width, imageSrc.size.height), imageSrc.CGImage);
CGImageRef imageRef = CGBitmapContextCreateImage(contextRef);
UIImage* imageDst = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
CGContextRelease(contextRef);
CGColorSpaceRelease(colorRef);
void CGContextClipToMask(CGContextRef c, CGRect rect, CGImageRef mask)
可以傳入一張部分透明的圖片去裁剪區(qū)域。相當(dāng)于蒙版曾我。
5. 一些簡(jiǎn)單應(yīng)用
5.1縮放圖片
UIImage *img;
CGFloat scale = 0.5;
CGSize size = CGSizeMake(img.size.width*scale,scale*img.size.height);
UIGraphicsBeginImageContext(size);
[img drawInRect:CGRectMake(0, 0, size.width, size.height)]; //這個(gè)方法會(huì)自動(dòng)轉(zhuǎn)換坐標(biāo)系
/*
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, 0, sz.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextDrawImage(ctx,CGRectMake(0, 0, size.width, size.height), img.CGImage);
*/
img = UIGraphicsGetImageFromCurrentImageContext();
CGContextRelease(ctx);
UIGraphicsEndImageContext();
return img;
5.2頭像裁剪成圓形
UIImage *img;
CGFloat radius = img.size.width;
UIGraphicsBeginImageContextWithOptions(img.size, NO, o);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, 0, sz.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextBeginPath(ctx);
CGContextAddArc(ctx, sz.width/2, sz.height/2, sz.width/2, 0, M_PI*2, YES);
CGContextClosePath(ctx);注意封口
CGContextEOClip(ctx);
CGContextDrawImage(ctx,CGRectMake(0, 0, sz.width, sz.height), img.CGImage);
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
CGContextRelease(ctx);
UIGraphicsEndImageContext();