【问题标题】:Why isn't my text being drawn correctly on retina dispaly为什么我的文字没有在视网膜显示器上正确绘制
【发布时间】:2014-07-17 07:53:22
【问题描述】:

我正在尝试绘制一些文本,它在非视网膜显示器上很好,但在视网膜显示器上它不是高分辨率的。

这是我的 iPad Air 的屏幕截图:

代码见下文,它实际上将文本绘制成半透明的,周围有一个几乎不透明的蒙版,因此您可以通过文本看到图像。

-(void) maskImage:(UIImageView *)imageView withText:(NSString *)text font:(UIFont *)font attributes:(NSDictionary *)attributes yOffset:(CGFloat)yOffset 
{ 
  NSStringDrawingContext *paraContext = [[NSStringDrawingContext alloc] init];
  NSStringDrawingOptions stringDrawingOptions = NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingTruncatesLastVisibleLine;

  // find out how much space we need for the text
  CGSize textSize = CGSizeIntegral([text boundingRectWithSize:CGSizeMake(kNowPlayingTrackDetailsMaxWidth, kNowPlayingTrackDetailsMaxHeight) options:stringDrawingOptions attributes:attributes context:paraContext].size);

  // set up the frame for drawing the text, including some padding

  CGSize drawingSize = {textSize.width + 80, textSize.height + 60};
  CGRect drawingFrame = { 0, 0, drawingSize.width, drawingSize.height};
  CGRect textFrame = { 40, 30, textSize.width, textSize.height };

  UIGraphicsBeginImageContextWithOptions(drawingSize, YES, 0);

  UIColor *bgColor = [UIColor colorWithWhite:1 alpha:.98];
  [bgColor setFill];
  [bgColor setStroke];

  UIBezierPath *path = [UIBezierPath bezierPathWithRect:drawingFrame];
  [path fill];

  // draw the text

  [text drawWithRect:textFrame options:stringDrawingOptions attributes:attributes context:paraContext];
  UIImage *textImage = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();

  // now create an image mask from the text image

  CIImage *inputImage = [CIImage imageWithCGImage:textImage.CGImage];
  CIFilter *filter = [CIFilter filterWithName:@"CIMaskToAlpha" keysAndValues:kCIInputImageKey, inputImage, nil];
  CIImage *outputImage = [filter outputImage];

  imageView.image = [UIImage imageWithCIImage:outputImage];

  // final set the UIImageView size to match the drawing size
  CGRect imageViewFrame = imageView.frame;
  imageViewFrame.size = drawingSize;
  imageView.frame = imageViewFrame;
}

【问题讨论】:

  • 遮罩的尺寸是多少,以像素为单位?我的意思是,您是否为视网膜分辨率放大了蒙版图像?
  • 您是否尝试过在像这样开始图像上下文时使用比例因子:UIGraphicsBeginImageContextWithOptions(drawingSize, YES, [[UIScreen mainScreen] scale]);
  • @holex,我很确定我到处都在使用点,如我的代码所示。
  • @AdilSoomro 我试过了。为 scale 参数传递 0 告诉 UIGraphics 使用主屏幕的比例。
  • outputImage的规模是多少?

标签: ios cocoa-touch nsstring uibezierpath uigraphicscontext


【解决方案1】:

Oliver Jones 帮我解决了这个问题。谢谢@orj!

答案是用

创建 UIImage
    [UIImage imageWithCIImage:outputImage scale:textImage.scale orientation:UIImageOrientationUp];

而不是

    imageView.image = [UIImage imageWithCIImage:outputImage];

这会正确设置输出图像的比例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    相关资源
    最近更新 更多