当前位置:   article > 正文

UIGraphicsBeginImageContext 和 UIGraphicsBeginImageContextWithOptions_uigraphicsbeginimagecontextwithoptions(imagesize,

uigraphicsbeginimagecontextwithoptions(imagesize, no, scale); imagesize 最大

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

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

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

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

效果如如下

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

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

来画图最好

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/799521
推荐阅读
相关标签
  

闽ICP备14008679号