UIGraphicsBeginImageContext 和 UIGraphicsBeginImageContextWithOptions

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/songzhuo1991/article/details/51850609

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

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

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

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

效果如如下

[objc]  view plain  copy
  1. // UIGraphicsBeginImageContext(self.projectImageView.frame.size);  
  2.     UIGraphicsBeginImageContextWithOptions(self.projectImageView.frame.sizeYES0.0);  
  3.     [image drawInRect:CGRectMake(00self.projectImageView.frame.size.widthself.projectImageView.frame.size.height)];  
  4.     CGContextRef context = UIGraphicsGetCurrentContext();  
  5.       
  6.     CGRect rect = CGRectMake(self.projectImageView.frame.size.width - 40,self.projectImageView.frame.size.height - 25,23,20);  
  7.     CGContextSetRGBFillColor(context, 0000.8);  
  8.     CGContextAddEllipseInRect(context, rect);  
  9.     CGContextDrawPath(context, kCGPathFill);  
  10.       
  11.     NSString *str=[NSString stringWithFormat:@"%zd",self.picNum];  
  12.     NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];  
  13.     paragraph.alignment = NSTextAlignmentCenter;  
  14.     [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}];  

[objc]  view plain  copy
  1. UIGraphicsBeginImageContext(self.projectImageView.frame.size);  
  2.    //UIGraphicsBeginImageContextWithOptions(self.projectImageView.frame.size, YES, 0.0);  
  3.    [image drawInRect:CGRectMake(00self.projectImageView.frame.size.widthself.projectImageView.frame.size.height)];  
  4.    CGContextRef context = UIGraphicsGetCurrentContext();  
  5.      
  6.    CGRect rect = CGRectMake(self.projectImageView.frame.size.width - 40,self.projectImageView.frame.size.height - 25,23,20);  
  7.    CGContextSetRGBFillColor(context, 0000.8);  
  8.    CGContextAddEllipseInRect(context, rect);  
  9.    CGContextDrawPath(context, kCGPathFill);  
  10.      
  11.    NSString *str=[NSString stringWithFormat:@"%zd",self.picNum];  
  12.    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];  
  13.    paragraph.alignment = NSTextAlignmentCenter;  
  14.    [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.sizeYES0.0);

扫描二维码关注公众号,回复: 3250222 查看本文章
来画图最好

猜你喜欢

转载自blog.csdn.net/songzhuo1991/article/details/51850609