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