目錄:
1、內(nèi)存中繪圖得到PNG圖片
2咬荷、由三張已知圖片拼得一張新圖片
內(nèi)存中繪圖得到PNG圖片(繪制好的圖片可以貼到imageview上冠句,背景是透明的,不影響其他內(nèi)容顯示)
- (UIImage*)drawImage:(NSArray *)arr
{
//這個是根據(jù)數(shù)組arr中傳入的點的坐標畫圖
// 創(chuàng)建內(nèi)存中的圖片(輸入圖片的寬和高)
UIGraphicsBeginImageContext(CGSizeMake(MWIDTH, 90));
// ---------下面開始向內(nèi)存中繪制圖形---------
//這里使用貝塞爾曲線畫圖
UIBezierPath *bezierPath2 = [[UIBezierPath alloc]init];
for (int i = 0; i < 8; i ++)
{
NSString *x = arr[i][@"x"];
NSString *y = arr[i][@"y"];
UIBezierPath *bezierPath1 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(x.floatValue, y.floatValue, 2, 2)];
//使用1畫紅色圓點
bezierPath1.lineWidth = 2;
[[UIColor redColor] setStroke];
[bezierPath1 stroke];
if (i == 0) {
[bezierPath2 moveToPoint:CGPointMake(x.floatValue, y.floatValue+1)];
}else{
[bezierPath2 addLineToPoint:CGPointMake(x.floatValue, y.floatValue+1)];
}
//使用2畫橘色連線
}
bezierPath2.lineWidth = 1;
[[UIColor orangeColor] setStroke];
[bezierPath2 stroke];
// 獲取該繪圖Context中的圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// ---------結(jié)束繪圖---------
UIGraphicsEndImageContext();
// 獲取當前應(yīng)用路徑中Documents目錄下的指定文件名對應(yīng)的文件路徑
NSString *path = [[NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"]
stringByAppendingPathComponent:@"newPng.png"];
NSLog(@"Path == %@",path);
// 保存PNG圖片
[UIImagePNGRepresentation(newImage) writeToFile:path atomically:YES];
//可以通過打印的路徑在文件夾中查看繪制的圖片
return newImage;
}
2幸乒、由三張已知圖片拼得一張新圖片
/**
* image拼圖 由上懦底、中、下三張圖片合并成一張圖片
*
* @param upImage 上面的圖片
* @param midImage 中間的圖片
* @param downImage 底部的圖片
* @param resultSize 需要返回的合并后的圖片的大小 注意不要小于三張圖片加起來的最小size
*
* @return
*/
+ (UIImage*)getImageFromUpImage:(UIImage *)upImage
midImage:(UIImage *)midImage
downImage:(UIImage *)downImage
withSize:(CGSize)resultSize
{
UIGraphicsBeginImageContextWithOptions(resultSize, NO, 0.0);
[upImage drawInRect:CGRectMake(0, 0,resultSize.width, upImage.size.height)];
[midImage drawInRect:CGRectMake(0, upImage.size.height,resultSize.width, resultSize.height - upImage.size.height-downImage.size.height)];
[downImage drawInRect:CGRectMake(0, resultSize.height - downImage.size.height,resultSize.width, downImage.size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}