UIGraphicsBeginImageContext 和 UIGraphicsBeginImageContextWithOptions

首先这两个方法都是用来进行获取图片的上下文,对这个图片进行绘制

但是在iphone的Retina屏幕上,如你使用UIGraphicsBeginImageContext这个方法来获取图形上下文进行绘制的话就会出现你绘制出来的图片相当的模糊,其实原因很简单

因为 UIGraphicsBeginImageContext(size) = UIGraphicsBeginImageContextWithOptions(size,NO,1.0)

那么UIGraphicsBeginImageContextWithOptions这个方法里面有3个属性,一个是size就是绘制的范围,还有一个是opaque,也就是这个图层是否完全透明,一般情况下最好设置为YES,这样可以让图层在渲染的时候效率更高。最关键的一个就是scale这个参数,那么这个参数的意思就是缩放比例,一般是1.0但是如果是在Retina屏幕上最好不要自己手动打个设置他的缩放比例,直接设置0,系统就会自动进行最佳的缩放

效果如如下

// UIGraphicsBeginImageContext(self.projectImageView.frame.size);
    UIGraphicsBeginImageContextWithOptions(self.projectImageView.frame.size, YES, 0.0);
    [image drawInRect:CGRectMake(0, 0, self.projectImageView.frame.size.width, self.projectImageView.frame.size.height)];
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGRect rect = CGRectMake(self.projectImageView.frame.size.width - 40,self.projectImageView.frame.size.height - 25,23,20);
    CGContextSetRGBFillColor(context, 0, 0, 0, 0.8);
    CGContextAddEllipseInRect(context, rect);
    CGContextDrawPath(context, kCGPathFill);
    
    NSString *str=[NSString stringWithFormat:@"%zd",self.picNum];
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.alignment = NSTextAlignmentCenter;
    [str drawInRect:CGRectMake(rect.origin.x, rect.origin.y + 2, rect.size.width, rect.size.height) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13],NSForegroundColorAttributeName:[UIColor whiteColor],NSParagraphStyleAttributeName : paragraph}];

 UIGraphicsBeginImageContext(self.projectImageView.frame.size);
    //UIGraphicsBeginImageContextWithOptions(self.projectImageView.frame.size, YES, 0.0);
    [image drawInRect:CGRectMake(0, 0, self.projectImageView.frame.size.width, self.projectImageView.frame.size.height)];
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGRect rect = CGRectMake(self.projectImageView.frame.size.width - 40,self.projectImageView.frame.size.height - 25,23,20);
    CGContextSetRGBFillColor(context, 0, 0, 0, 0.8);
    CGContextAddEllipseInRect(context, rect);
    CGContextDrawPath(context, kCGPathFill);
    
    NSString *str=[NSString stringWithFormat:@"%zd",self.picNum];
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.alignment = NSTextAlignmentCenter;
    [str drawInRect:CGRectMake(rect.origin.x, rect.origin.y + 2, rect.size.width, rect.size.height) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13],NSForegroundColorAttributeName:[UIColor whiteColor],NSParagraphStyleAttributeName : paragraph}];

大家可以明显的看到像素差距很大,所以今后在画图的时候最好使用

UIGraphicsBeginImageContextWithOptions(self.projectImageView.frame.size, YES, 0.0);

来画图最好

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值