我正在尝试绘制一些文字,并且在非视网膜显示器上很好,但在视网膜显示器上它不是高分辨率。
这是我的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;
}
答案 0 :(得分:1)
奥利弗·琼斯帮我解决了这个问题。谢谢@orj!
答案是用
创建UIImage [UIImage imageWithCIImage:outputImage scale:textImage.scale orientation:UIImageOrientationUp];
而不是
imageView.image = [UIImage imageWithCIImage:outputImage];
这样可以正确设置输出图像的比例。