开发者

NSString drawAtPoint - blurry

I have been trying to add an NSString to an NSImage in my applicatio开发者_C百科n. 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.


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.


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.


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].


Just had this problem. This is all you need:

CGContextSetShouldAntialias(yourcontext, true);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜