• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 为什么我的文字没有在视网膜显示器上正确绘制

[复制链接]
菜鸟教程小白 发表于 2022-12-12 23:50:48 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

enter image description here

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

-(void) maskImageUIImageView *)imageView withTextNSString *)text fontUIFont *)font attributesNSDictionary *)attributes yOffsetCGFloat)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 imageWithCIImageutputImage];

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



Best Answer-推荐答案


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

答案是用

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

而不是

    imageView.image = [UIImage imageWithCIImageutputImage];

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

关于ios - 为什么我的文字没有在视网膜显示器上正确绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24797815/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap