1

にテキストを追加しようとしています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];
}

ギザギザの画像の例

4

2 に答える 2

5

ピーターが言ったように、を使用してUIGraphicsBeginImageContextWithOptionsください。image.scaleをscale属性(image.scaleは描画している画像の1つのスケール)に渡すか、単に[UIScreenmainScreen].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;

textNSStringオブジェクトはどこにありますか。

于 2013-01-08T12:58:10.743 に答える
0

私はまったく同じ問題に直面していて、昨夜誰も答えなかった質問を投稿しました。次の質問で、fnfの提案した変更とClif Viegasの回答を組み合わせました。CGContextDrawImageは、 UIImage.CGImageを渡すと画像を上下逆に描画します。

解決策を考え出すために。私のaddTextメソッドはあなたのものとは少し異なります:

+(UIImage *)addTextToImage:(UIImage *)img text:(NSString *)text1 {

int w = img.size.width;
int h = img.size.height;

CGSize size = CGSizeMake(w, h);
if (UIGraphicsBeginImageContextWithOptions != NULL) {
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
} else {
    UIGraphicsBeginImageContext(size);
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();


CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0,h);
CGContextScaleCTM(context, 1.0, -1.0);

CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);

CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);

char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];// \"05/05/09\";

CGContextSelectFont(context, "Times New Roman", 14, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);

CGContextSetRGBFillColor(context, 0, 0, 0, 1);

//rotate text

CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/8 ));

CGContextShowTextAtPoint(context, 70, 88, text, strlen(text));

CGColorSpaceRelease(colorSpace);

UIGraphicsEndImageContext();
newImgを返します。

}

要約すると、私がしたことは追加です

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

削除する

// CGContextRef context = CGBitmapContextCreate(NULL、w、h、8、4 * w、colorSpace、kCGImageAlphaPremultipliedFirst);

そして、置きます

CGContextRef context = UIGraphicsGetCurrentContext();

代わりは。ただし、これにより画像が上下逆になります。したがって、私は置きます

CGContextTranslateCTM(context, 0,h);
CGContextScaleCTM(context, 1.0, -1.0);

CGContextDrawImage(context、CGRectMake(0、0、w、h)、img.CGImage);の直前

このようにして、drawInRectなどを使用する代わりにCG関数を使用できます。

忘れないで

UIGraphicsEndImageContext

最後に。お役に立てれば。

于 2013-01-08T23:07:25.017 に答える