NSString drawAtPoint - blurry

五迷三道 提交于 2020-01-03 01:45:06

问题


I have been trying to add an NSString to an NSImage in my application. For some reason it seems to be extremely blurry and noisy. Here is the output that is saved as a jpeg.

http://i.stack.imgur.com/QHVQF.jpg

I have read that it might have been due to drawing it at a point that is not rounded numbers, but it doesn't seem to change anything. I made sure the cords are exactly integers.

Here is how I add the text:

[generatedRectangle lockFocus];
//Draw according to the settings
[color set];
NSRectFill(NSMakeRect(0, 0, sizeX, sizeY));


NSPoint p = NSMakePoint(round(sizeX/2), round(sizeY/2));

[text drawAtPoint:p withAttributes:NULL];

Then I save it by capturing it with an NSBitmapImageRep and pass it to the save panel. I actually write the file by this command:

[[generatedRect representationUsingType: NSJPEGFileType properties: nil]
   writeToFile:[save filename] atomically: YES])

Any ideas on why the text is so blurry? I ran out of ideas a good bit ago.

All help is greatly appreciated.


回答1:


Be aware that Quartz draws on mid-pixel boundaries, so you may need to make sure your -drawAtPoint:withAttributes: coordinate is not integral, but offset by a half pixel in X and Y (for example: 10.5, 10.5 instead of 10.0, 10.0).

Also, make sure that views in your hierarchy being drawn out all have their origins on integral pixel boundaries. Even one superview of your text-drawing view being off can lead to blurry rendering. You may be able to use the Core Animation instrument's Color Misaligned Layers option to track this down, if these views are layer-backed.




回答2:


The issue with mine was not using the proper scale factor for retina displays when I created the context to draw in:

UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0f);

If you use UIGraphicsBeginImageContext(<#CGSize size#>) then that is the iOS 4.0 and lower API so it'll draw it as if you're not on a retina display.




回答3:


That looks to me like excessive JPEG compression. You can specify the amount of compression by the NSImageCompressionFactor property. To do no compression, set it to [NSNumber numberWithFloat:1.0].




回答4:


Just had this problem. This is all you need:

CGContextSetShouldAntialias(yourcontext, true);



来源:https://stackoverflow.com/questions/4670851/nsstring-drawatpoint-blurry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!