画图至 UIImage ,而后再在 drawRect 里面,贴上UIImage,图片模糊的问题
解决方式:
UIGraphicsBeginImageContext(CGSizeMake(maxWidth*2, maxHeight*2));
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat scale = [[UIScreen mainScreen] scale];
CGContextScaleCTM(UIGraphicsGetCurrentContext(), scale, scale);
。。。。
return [UIImage imageWithCGImage:returnImg.CGImage scale:scale orientation:UIImageOrientationUp];
附带上一个函数
//得到合成图片
-(UIImage*)getImage:(UIImage*)image text:(NSString*)text textColor:(UIColor*)color backgroundImage:(UIImage*)backImg size:(CGSize)size
{
UIImage* returnImg = nil;
CGSize imageSize = image.size;
CGFloat maxWidth = size.width;
CGFloat maxHeight = size.height;
// 创建一个bitmap的context
CGFloat scale = [[UIScreen mainScreen] scale];
UIGraphicsBeginImageContext(CGSizeMake(maxWidth*scale, maxHeight*scale));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextScaleCTM(UIGraphicsGetCurrentContext(), scale, scale);
// CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextClearRect(context,CGRectMake(0, 0, maxWidth, maxHeight));
// CGContextFillRect(context,CGRectMake(0, 0, maxWidth, maxHeight));
// 绘制图片
[backImg drawInRect:CGRectMake(0, 0, maxWidth,maxHeight)]; //背景
CGRect rcImage = CGRectMake((maxWidth-imageSize.width)/2, 6, imageSize.width,imageSize.height);
[image drawInRect:rcImage];//前景
// 绘制文字
// CGContextSetStrokeColorWithColor(context, color.CGColor);
CGSize sizeTextCanDraw = [text sizeWithFont:[self getFont] forWidth:size.width lineBreakMode:UILineBreakModeWordWrap];
CGContextSetFillColorWithColor(context, color.CGColor);
CGRect rcTextRect = CGRectMake(0, size.height - sizeTextCanDraw.height - 8, size.width, sizeTextCanDraw.height);
[text drawInRect:rcTextRect withFont:[self getFont] lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
// 从当前context中创建一个改变大小后的图片
returnImg = UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();
return [UIImage imageWithCGImage:returnImg.CGImage scale:scale orientation:UIImageOrientationUp];
}