多图生成一张图片,给图片添加文字
2016-06-24 本文已影响549人
程序猿小武
#pragma mark--------------规律的图片数组生成一张图--------------------------
/**
* <#Description#>
*
* @param imageArr 图片数组
* @param imageV imageView的尺寸
*
* @return 处理过后的image
*/
- (UIImage *)addImage:(NSArray *)imageArr toImageV:(UIImageView *)imageV {
UIGraphicsBeginImageContext(imageV.frame.size);
for (int a=0; a<imageArr.count; a++) {
// Draw image1
[imageArr[a] drawInRect:CGRectMake(0, a * 300, imageV.frame.size.width, 300)];
}
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [self watermarkImage:resultingImage withText:@"这就是水印"];
}
#pragma mark--------------不规律的图片数组生成一张图--------------------------
/**
* <#Description#>
*
* @param image1 图片1
* @param image2 图片2
*
* @return 将图片2放在图片1上返回
*/
- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {
UIGraphicsBeginImageContext(image1.size);
// Draw image1
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
// Draw image2
[image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
#pragma mark--------------给图片绘制文字--------------------------
/**
* <#Description#>
*
* @param img 需要处理的图片
* @param text 添加的文字
*
* @return 处理过后的图片
*/
- (UIImage *)watermarkImage:(UIImage *)img withText:(NSString *)text{
//1.获取上下文
UIGraphicsBeginImageContext(img.size);
//2.绘制图片
[img drawInRect:CGRectMake(0, 0, img.size.width, img.size.height)];
//3.绘制水印文字
CGRect rect = CGRectMake(0,20, img.size.width, 20);
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
style.alignment = NSTextAlignmentCenter;
//文字的属性
NSDictionary *dic = @{
NSFontAttributeName:[UIFont systemFontOfSize:18],
NSParagraphStyleAttributeName:style,
NSForegroundColorAttributeName:[UIColor whiteColor]
};
//将文字绘制上去
[text drawInRect:rect withAttributes:dic];
//4.获取绘制到得图片
UIImage *watermarkImage = UIGraphicsGetImageFromCurrentImageContext();
//5.结束图片的绘制
UIGraphicsEndImageContext();
return watermarkImage;
}
#pragma mark--------------将文字准换成图片--------------------------
#define CONTENT_MAX_WIDTH 300.0f
-(UIImage *)imageFromText:(NSArray*) arrContent withFont: (CGFloat)fontSize
{
// set the font type and size
UIFont *font = [UIFont systemFontOfSize:fontSize];
NSMutableArray *arrHeight = [[NSMutableArray alloc] initWithCapacity:arrContent.count];
CGFloat fHeight = 0.0f;
for (NSString *sContent in arrContent) {
CGSize stringSize = [sContent sizeWithFont:font constrainedToSize:CGSizeMake(CONTENT_MAX_WIDTH, 10000) lineBreakMode:UILineBreakModeWordWrap];
[arrHeight addObject:[NSNumber numberWithFloat:stringSize.height]];
fHeight += stringSize.height;
}
CGSize newSize = CGSizeMake(CONTENT_MAX_WIDTH+20, fHeight+50);
UIGraphicsBeginImageContextWithOptions(newSize,NO,0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetCharacterSpacing(ctx, 10);
CGContextSetTextDrawingMode (ctx, kCGTextFillStroke);
CGContextSetRGBFillColor (ctx, 0.1, 0.2, 0.3, 1); // 6
CGContextSetRGBStrokeColor (ctx, 0, 0, 0, 1);
int nIndex = 0;
CGFloat fPosY = 20.0f;
for (NSString *sContent in arrContent) {
NSNumber *numHeight = [arrHeight objectAtIndex:nIndex];
CGRect rect = CGRectMake(10, fPosY, CONTENT_MAX_WIDTH , [numHeight floatValue]);
[sContent drawInRect:rect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
fPosY += [numHeight floatValue];
nIndex++;
}
// transfer image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}