1雳刺、UIImage顯示靜止圖片的類方法
* +imageNamed: 該方法有緩存機制,如果需要頻繁的加載卸載圖片時裸违,不應該使用該方法
* + imageWithContentOfFile: 該方法加載指定文件名對應的圖片
* + imageWithData:
* + imageWithData:scale: 指定縮放因子對圖片進行縮放
* + imageWithCGImages:根據(jù)指定的CGImageRef對象來創(chuàng)建UIImage
* + imageWithCGImages:scale:orientation:
* + animatedImageNamed:duration: 根據(jù)指定的圖片名在加載系列圖片
* + animatedImageWithImages:duration: 該方法需要傳入一個NSArray作為多張動畫圖片
動畫效果
- CGAffineTransformMake
CGAffineTransform CGAffineTransformMake (
CGFloat a,
CGFloat b,
CGFloat c,
CGFloat d,
CGFloat tx,
CGFloat ty );
//創(chuàng)建一個給定比例放縮的變換
CGAffineTransformMakeScale (CGFloat sx, CGFloat sy);
CGAffineTransformMakeScale(-1.0, 1.0);//水平翻轉(zhuǎn)
CGAffineTransformMakeScale(1.0,-1.0);//垂直翻轉(zhuǎn)
//創(chuàng)建一個旋轉(zhuǎn)角度的變化
CGAffineTransform CGAffineTransformMakeRotation ( CGFloat angle);
//創(chuàng)建一個平移的變化
CGAffineTransform CGAffineTransformMakeTranslation (CGFloat tx,CGFloat ty);
img
六個參數(shù)對應矩陣的前兩列掖桦。
- (void)transformImageView
{
CGAffineTransform t = CGAffineTransformMakeScale(scale * previousScale,
scale * previousScale);
t = CGAffineTransformRotate(t, rotation + previousRotation);
self.imageView.transform = t;
}
// 繪制復雜的圖形,必須啟用路徑
在Canvas(畫布)中使用路徑供汛,可按如下步驟進行
- 1枪汪、調(diào)用CGContextBeginPath()函數(shù)開始定義路徑
- 2、 調(diào)用表12.4所示的各種函數(shù)添加子路徑
- 3紊馏、如果路徑添加完成料饥,調(diào)用CGContextClosePath()函數(shù)關閉路徑
- 4、調(diào)用CGContextDrawPath()朱监、CGContextEOFillPath()岸啡、CGContextFillPath()、CGContextStrokePath()函數(shù)來填充路徑赫编,或繪制路徑邊框巡蘸,第一個方法可以替代后面幾個,設置特定的模式
在內(nèi)存中繪圖
步驟如下
- 1擂送、調(diào)用UIGraphicsBeginImageContext(<#CGSize size#>)函數(shù)準備繪圖環(huán)境
- 2悦荒、UIGraphicsGetCurrentContext()函數(shù)獲取繪圖CGContextRef
- 3、用前面介紹的繪制集合圖形嘹吨、使用路徑等方式進行繪圖
- 4搬味、UIGraphicsGetImageFromCurrentImageContext()獲取當前繪制圖形,該方法返回一個UIImage對象
- 5蟀拷、UIGraphicsEndImageContext()結(jié)束繪圖碰纬,并關閉繪圖環(huán)境
圖形變換
Quartz 2D提供如下坐標變換
- CGContextTranslateCTM(CGContextRef c, CGFloat tx, CGFloat ty),平移坐標系,把(0问芬,0)位置的坐標原點平移到(tx,ty)
- CGContextScaleCTM(CGContextRef c, CGFloat sx, CGFloat sy)縮放sx悦析,sy
- CGContextRotateCTM(CGContextRef c, CGFloat angle),旋轉(zhuǎn)坐標angle弧度
- CGContextSaveGState(CGContextRef c) //保存當前的繪圖狀態(tài)
CGContextRestoreGState(CGContextRef c) // 恢復之前保存的繪圖狀態(tài)
使用矩陣變換
- CGContextConcatCTM(CGContextRef c, CGAffineTransform transform)通過坐標矩陣變換
- CGAffineTransform CGContextGetCTM(CGContextRef c)獲取坐標系統(tǒng)的變換矩陣,CGAffineTransform t 代表變換矩陣
- 創(chuàng)建CGAffineTransform可以使用如下函數(shù)進行:
CGAffineTransformMakeTranslation(CGFloat tx, CGFloat ty);位移變換的矩陣
CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)縮放變換
CGAffineTransformMakeRotation(CGFloat angle)角度變換
CGAffineTransformMake(CGFloat a, CGFloat b, CGFloat c, CGFloat d, CGFloat tx, CGFloat ty),該函數(shù)使用自定義變換矩陣此衅,其中(a,b,c,d)將會組成變換矩陣,變換后實際坐標(xa + yc + tx, xb + yd + ty)
比如要進行水平鏡像(繞Y軸做對稱變化)强戴,此時變換矩陣為CGAffineTransformMake(-1, 0, 0, 1, 0, 0)
Core Image 濾鏡
三個核心API
- CIContext:所有的圖片處理都在它的管理下完成
- CIFilter:代表過濾器,在創(chuàng)建CIFilter時需要傳入不同的參數(shù)即可創(chuàng)建不同類型的過濾器
- CIImage:代表處理的圖片
使用例子
1.創(chuàng)建CIContext對象挡鞍。有三種創(chuàng)建方式
// 第一種創(chuàng)建方式:基于CPU的CIContext對象
CIContext *ctx = [CIContext contextWithOptions:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],kCIContextUseSoftwareRenderer, nil]];
// 第二種創(chuàng)建方式:基于GPU的CIContext對象
ctx = [CIContext contextWithOptions:nil];
// 第三種方式:基于OpenGL優(yōu)化的對象
EAGLContext *eaglctx = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
2.創(chuàng)建CIFilter過濾器骑歹,CIFilter提供了filterWithName:類方法來創(chuàng)建CIFilter對象,該方法需要傳入過濾器的名字
濾鏡名字查詢
NSArray *names = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
NSLog(@"%@",names);
通過打印可以獲取濾鏡的所有名字
3.創(chuàng)建CIImage對象
4.調(diào)用CIFilter的setValue方法為inputImage屬性復制
[filter setValue:image forKey:kCIInputImageKey];
[filter setValue:[CIColor colorWithRed:100/255 green:0.4 blue:1] forKey:kCIInputColorKey];
5.根據(jù)需要墨微,為不同的濾鏡設置不同的過濾參數(shù)
6.調(diào)用CIFilter的outputImage方法獲取處理后的圖片
Core Animation動畫基礎
使用core animaton創(chuàng)建動畫陵刹,不僅簡單而且具有更好的性能,原因如下:
- core animaton動畫在單獨的線程完成,不會阻塞主線程
- core animaton動畫只會重繪界面上變化的部分(局部刷新)
Core Animation動畫還涉及如下API
- CAAnimation:它是所有動畫的基類
- CATransition:
- CAPropertyAnimation:屬性動畫
- CABasicAnimation: CAPropertyAnimation的子類
- CAKeyframeAnimation:
- CAAnimationGroup: