0x00 步驟
- 先讀取兩張圖片把創(chuàng)建出CGImageRef
- 創(chuàng)建上下文畫布
- 把圖片依次畫在畫布指定位置上
- 從上下文中獲得合并后的圖片
- 關(guān)閉上下文
- 釋放內(nèi)存
0x01 代碼實現(xiàn)
- (void)composeImg {
UIImage *img = [UIImage imageNamed:@"0.png"];
CGImageRef imgRef = img.CGImage;
CGFloat w = CGImageGetWidth(imgRef);
CGFloat h = CGImageGetHeight(imgRef);
//以1.png的圖大小為底圖
UIImage *img1 = [UIImage imageNamed:@"1.png"];
CGImageRef imgRef1 = img1.CGImage;
CGFloat w1 = CGImageGetWidth(imgRef1);
CGFloat h1 = CGImageGetHeight(imgRef1);
//以1.png的圖大小為畫布創(chuàng)建上下文
UIGraphicsBeginImageContext(CGSizeMake(w1, h1));
[img1 drawInRect:CGRectMake(0, 0, w1, h1)];//先把1.png 畫到上下文中
[img drawInRect:CGRectMake(100, 100, w, h)];//再把小圖放在上下文中
UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();//從當前上下文中獲得最終圖片
UIGraphicsEndImageContext();//關(guān)閉上下文
NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [path stringByAppendingPathComponent:@"01.png"];
[UIImagePNGRepresentation(resultImg) writeToFile:filePath atomically:YES];//保存圖片到沙盒
CGImageRelease(imgRef);
CGImageRelease(imgRef1);
}
0x10 效果圖
0.png
1.png
01.png