Ios 文本赢得';t显示在外部位图/jpeg文件中。Drawatpoint在当前上下文中不工作?

Ios 文本赢得';t显示在外部位图/jpeg文件中。Drawatpoint在当前上下文中不工作?,ios,text,core-graphics,Ios,Text,Core Graphics,对CoreGraphics来说相当陌生,但掌握了基本技巧。尝试写入外部文件(而不是手机屏幕)。显示所有网格,但它不会使用drawAtPoint渲染文本。不管我怎么做都不行 下面是我的代码,它在viewDidLoad时触发: - (void)viewDidLoad { CGFloat width = 312; CGFloat height = 482; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

对CoreGraphics来说相当陌生,但掌握了基本技巧。尝试写入外部文件(而不是手机屏幕)。显示所有网格,但它不会使用drawAtPoint渲染文本。不管我怎么做都不行

下面是我的代码,它在viewDidLoad时触发:

- (void)viewDidLoad {

    CGFloat width = 312;
    CGFloat height = 482;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    size_t bitsPerComponent = 8;
    size_t bytesPerPixel    = 4;
    size_t bytesPerRow      = (width * bitsPerComponent * bytesPerPixel + 7) / 8;
    size_t dataSize         = bytesPerRow * height;

    unsigned char *data = malloc(dataSize);
    memset(data, 0, dataSize);

    CGContextRef context = CGBitmapContextCreate(data, width, height, 
                                                 bitsPerComponent, 
                                                 bytesPerRow, colorSpace, 
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

    // Flip coordinate system
    CGRect bounds = CGContextGetClipBoundingBox(context);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextTranslateCTM(context, 0.0, -bounds.size.height);

    //  And now the magic happens...

    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
    CGContextFillRect(context, CGRectMake(0,0,312,482));

    int tableTop=54;
    int cellsPerColumn=26;
    int cellHeight=15;

    [@"Title" drawAtPoint:CGPointMake(8, 8) withFont:[UIFont boldSystemFontOfSize:9.0f]];   //  Doesn't print text on  screen?!

    for(int a=1;a<cellsPerColumn+1;a++)
    {
        CGContextStrokeRect(context, CGRectMake(8,(tableTop+cellHeight*(a+1)),60,cellHeight));
        CGContextStrokeRect(context, CGRectMake(66,(tableTop+cellHeight*(a+1)),85,cellHeight));
        CGContextStrokeRect(context, CGRectMake(159,(tableTop+cellHeight*(a+1)),60,cellHeight));
        CGContextStrokeRect(context, CGRectMake(217,(tableTop+cellHeight*(a+1)),85,cellHeight));
    }



    //  Wrap up context and produce the image...



    CGColorSpaceRelease(colorSpace);
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    UIImage *result = [[UIImage imageWithCGImage:imageRef] retain];
    CGImageRelease(imageRef);
    CGContextRelease(context);

    free(data);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"user_images/myImage.jpg"];

    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(result)];
    [imageData writeToFile:filePath atomically:YES];
    [super viewDidLoad];
}
-(void)viewDidLoad{
CGFloat宽度=312;
CGFloat高度=482;
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
大小\u t比特分量=8;
每像素大小×字节=4;
size_t bytesPerRow=(宽度*比特组件*字节/像素+7)/8;
尺寸=字节数*高度;
无符号字符*数据=malloc(数据大小);
memset(数据,0,数据大小);
CGContextRef context=CGBitmapContextCreate(数据、宽度、高度、,
比特组件,
bytesPerRow,色彩空间,
KCGIMAGEAlphaPremultipledLast | kCGBitmapByteOrder32Big);
//翻转坐标系
CGRect bounds=CGContextGetClipboondingBox(上下文);
CGContextScaleCTM(上下文,1.0,-1.0);
CgContextTranslateCm(上下文,0.0,-bounds.size.height);
//现在奇迹发生了。。。
CGContextSetRGBFillColor(上下文,1.0,1.0,1.0,1.0);
CGContextFillRect(上下文,CGRectMake(0,0312482));
int桌面=54;
int cellsPerColumn=26;
int cellHeight=15;
[@“Title”drawAtPoint:CGPointMake(8,8)with font:[UIFont boldSystemFontOfSize:9.0f]];//不在屏幕上打印文本?!

对于(int a=1;a实用程序方法,如drawAtPoint:仅适用于在当前图形上下文中绘制,通常是在子类NSView的drawRect回调期间提供的上下文。或者,您可以使用创建的位图上下文尝试setCurrentContext:方法。

好的,谢谢您的回答,但让我想到可能我正在继续这完全是错误的。我正在尝试在手机上生成一个包含绘图对象、图像和文本的文件,使用NS实用程序命令,如drawAtPoint。该文件的尺寸超出屏幕范围,需要通过编程和幕后生成该文件,以便用户无需查看它。I al因此,我想控制生成图像的分辨率(pref 300dpi)。必须有一个简单的方法来做到这一点?非常感谢任何帮助。问候,Rich