Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone CGContextShowTextAtPoint在视网膜设备上产生非视网膜输出_Iphone_Ios_Core Graphics - Fatal编程技术网

Iphone CGContextShowTextAtPoint在视网膜设备上产生非视网膜输出

Iphone CGContextShowTextAtPoint在视网膜设备上产生非视网膜输出,iphone,ios,core-graphics,Iphone,Ios,Core Graphics,我正试图将文本添加到UIImage中,我得到了一个像素化的图形 我尝试了其他一些答案,但没有成功: 我的代码: -(UIImage *)addText:(UIImage *)imgV text:(NSString *)text1 { int w = self.frame.size.width * 2; int h = self.frame.size.height * 2; CGColorSpaceRef colorSpace = CGColorSpaceCreateD

我正试图将文本添加到
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];
}

如Peter所说,使用带有选项的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;

其中,
text
是一个NSString对象。

如Peter所说,使用
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;

其中,
text
是一个NSString对象。

我遇到了完全相同的问题,昨晚发布了一个没有人回答的问题。我将fnf建议的变更与Clif Viegas在以下问题中的回答结合起来:

想出解决办法。我的addText方法与您的略有不同:

+(UIImage*)添加文本到图像:(UIImage*)img文本:(NSString*)文本1{

UIGraphicsSendImageContext();
返回newImg

}

总之,我所做的是添加

if (UIGraphicsBeginImageContextWithOptions != NULL) {
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
} else {
    UIGraphicsBeginImageContext(size);
}
除去

//CGContextRef context=CGBitmapContextCreate(NULL,w,h,8,4*w,colorSpace,KCGIMAGEAlphaPremultipledFirst)

CGContextRef context = UIGraphicsGetCurrentContext();
相反。但这会导致图像颠倒。因此我提出

CGContextTranslateCTM(context, 0,h);
CGContextScaleCTM(context, 1.0, -1.0);
就在之前 CGContextDrawImage(上下文,CGRectMake(0,0,w,h),img.CGImage)

通过这种方式,您可以使用CG函数,而不是使用drawInRect等

别忘了

UIGraphicsSendImageContext


最后。希望这能有所帮助。

我昨晚遇到了完全相同的问题,并发布了一个没有人回答的问题。我将fnf建议的变更与Clif Viegas在以下问题中的回答结合起来:

想出解决办法。我的addText方法与您的略有不同:

+(UIImage*)添加文本到图像:(UIImage*)img文本:(NSString*)文本1{

UIGraphicsSendImageContext();
返回newImg

}

总之,我所做的是添加

if (UIGraphicsBeginImageContextWithOptions != NULL) {
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
} else {
    UIGraphicsBeginImageContext(size);
}
除去

//CGContextRef context=CGBitmapContextCreate(NULL,w,h,8,4*w,colorSpace,KCGIMAGEAlphaPremultipledFirst)

CGContextRef context = UIGraphicsGetCurrentContext();
相反。但这会导致图像颠倒。因此我提出

CGContextTranslateCTM(context, 0,h);
CGContextScaleCTM(context, 1.0, -1.0);
就在之前 CGContextDrawImage(上下文,CGRectMake(0,0,w,h),img.CGImage)

通过这种方式,您可以使用CG函数,而不是使用drawInRect等

别忘了

UIGraphicsSendImageContext


最后。希望这有帮助。

您是否考虑过使用
UIGraphicsBeginImageContextWithOptions
和相关函数?是的,但文本的边缘保持锯齿状。您为比例因子传递了哪个值?另外,请编辑您的问题,以包含“像素化”、“锯齿状”文本的屏幕截图。那么,您将哪个比例因子值传递给了获得相同结果的
UIGraphicsBeginImageContextWithOptions
?正确的值为
0.0
,它将自动检测主屏幕的正确比例因子。但是如果您尝试了
2.0
,但没有效果,那么您的问题一定是我不知道的其他问题。您是否考虑过使用
UIGraphicsBeginImageContextWithOptions
和相关函数?是的,但文本的边缘保持锯齿状。您传递了哪个值作为比例因子?另外,请编辑您的问题,以包含“像素化”、“锯齿状”文本的屏幕截图。那么,您将哪个比例因子值传递给了获得相同结果的
UIGraphicsBeginImageContextWithOptions
?正确的值为
0.0
,它将自动检测主屏幕的正确比例因子。但是如果您尝试了
2.0
,但没有效果,那么您的问题一定是我不知道的其他问题。