赞
踩
首先这两个方法都是用来进行获取图片的上下文,对这个图片进行绘制
但是在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);
来画图最好Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。