画图至 UIImage ,而后再在 drawRect 里面,贴上UIImage,图片模糊的问题

画图至 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];
   
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值