iphone 中字符串生成纹理图片

- (void) getBitmapfromText:(NSString *)text iSize:(CGSize)imageSize { // Create a bitmap graphics context of the given size CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); CGContextRef context = CGBitmapContextCreate(NULL, imageSize.width, imageSize.height,8, imageSize.width, colorSpace, kCGImageAlphaNone); CGColorSpaceRelease(colorSpace); if (context== NULL) { fprintf (stdout, "Context not created!"); } // Custom CGContext coordinate system is flipped with respect to UIView, so transform, then push CGContextTranslateCTM(context, 0, imageSize.height); CGContextScaleCTM(context, 1.0, -1.0); UIGraphicsPushContext(context); // Inset the text rect then draw the text CGRect textRect = CGRectMake(1, 1, imageSize.width - 2, imageSize.height - 2); UIFont *font = [UIFont boldSystemFontOfSize:12]; [[UIColor blackColor] set]; [text drawInRect:textRect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter]; // Create and return the UIImage object CGImageRef imageRef = CGBitmapContextCreateImage(context); // 方法2 用到了这个 uiimg = [[UIImage alloc] initWithCGImage:imageRef]; // 这里uiimg是个全局变量 UIGraphicsPopContext(); CGContextRelease(context); CGImageRelease(imageRef); }

 

熟悉windows下GDI编程的同志一看就知道, 方法有点类似。

你可能感兴趣的:(iphone 中字符串生成纹理图片)