CGContextShowTextAtPoint在Retina设备上产生非Retina输出

ppa*_*ojr 1 iphone core-graphics ios

我正在尝试添加一个文本,UIImage我得到一个像素化的绘图.

我尝试了其他一些没有成功的答案:

我的代码:

-(UIImage *)addText:(UIImage *)imgV text:(NSString *)text1
{
   int w = self.frame.size.width * 2;
   int h = self.frame.size.height * 2;
   CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   CGContextRef context = CGBitmapContextCreate
         (NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

   CGContextDrawImage(context, CGRectMake(0, 0, w, h), imgV.CGImage);    
   CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);

   char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
   CGContextSelectFont(context, "Arial", 24, kCGEncodingMacRoman);

   // Adjust text;

   CGContextSetTextDrawingMode(context, kCGTextInvisible);
   CGContextShowTextAtPoint(context, 0, 0, text, strlen(text));

   CGPoint pt = CGContextGetTextPosition(context);
   float posx = (w/2 - pt.x)/2.0;
   float posy = 54.0;    
   CGContextSetTextDrawingMode(context, kCGTextFill);
   CGContextSetRGBFillColor(context, 255, 255, 255, 1.0);

   CGContextShowTextAtPoint(context, posx, posy, text, strlen(text));

   CGImageRef imageMasked = CGBitmapContextCreateImage(context);
   CGContextRelease(context);
   CGColorSpaceRelease(colorSpace);

   return [UIImage imageWithCGImage:imageMasked];
}
Run Code Online (Sandbox Code Playgroud)

锯齿状图像的例子

fnf*_*fnf 5

就像彼得说的那样,使用UIGraphicsBeginImageContextWithOptions.您可能希望将image.scale传递给scale属性(其中image.scale是您正在绘制的图像之一的比例),或者只是使用[UIScreen mainScreen] .scale.

整个代码可以更简单.尝试类似的东西:

// Create the image context
UIGraphicsBeginImageContextWithOptions(_baseImage.size, NO, _baseImage.scale);

// Draw the image
CGRect rect = CGRectMake(0, 0, _baseImage.size.width, _baseImage.size.height);
[_baseImage drawInRect:rect];

// Get a vertically centered rect for the text drawing
rect.origin.y = (rect.size.height - FONT_SIZE) / 2 - 2;
rect = CGRectIntegral(rect);
rect.size.height = FONT_SIZE;

// Draw the text
UIFont *font = [UIFont boldSystemFontOfSize:FONT_SIZE];
[[UIColor whiteColor] set];
[text drawInRect:rect withFont:font lineBreakMode:NSLineBreakByClipping alignment:NSTextAlignmentCenter];

// Get and return the new image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
Run Code Online (Sandbox Code Playgroud)

textNSString对象在哪里.


归档时间:

查看次数:

960 次

最近记录:

12 年,11 月 前