Iphone 如何在图像上下文中绘制图像和文本?
我尝试在图像上绘制文本。当我不应用转换时,图像将在左下角绘制,图像和文本将如图2所示,但我希望图像位于视图的左上角 下面是我的drawRect实现 如何翻转图像,使文本和图像正确对齐 或 如何将图像移动到视图的左上角 如果我不使用下面的函数调用,就会在视图的底部创建图像Iphone 如何在图像上下文中绘制图像和文本?,iphone,ios,objective-c,core-graphics,Iphone,Ios,Objective C,Core Graphics,我尝试在图像上绘制文本。当我不应用转换时,图像将在左下角绘制,图像和文本将如图2所示,但我希望图像位于视图的左上角 下面是我的drawRect实现 如何翻转图像,使文本和图像正确对齐 或 如何将图像移动到视图的左上角 如果我不使用下面的函数调用,就会在视图的底部创建图像 CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, self.bounds.size.height); CGContextScaleCTM(UIGraphicsGe
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, self.bounds.size.height);
CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
- (void)drawRect:(CGRect)rect
{
UIGraphicsBeginImageContext(self.bounds.size);
// Fig 2 comment these lines to have Fig 2
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, self.bounds.size.height);
CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
// Fig 2 Applying the above transformations results in Fig 1
UIImage *natureImage = [UIImage imageNamed:@"Nature"];
CGRect natureImageRect = CGRectMake(130, 380, 50, 50);
[natureImage drawInRect:natureImageRect];
UIFont *numberFont = [UIFont systemFontOfSize:28.0];
NSFileManager *fm = [NSFileManager defaultManager];
NSString * aNumber = @"111";
[aNumber drawAtPoint:CGPointMake(100, 335) withFont:numberFont];
UIFont *textFont = [UIFont systemFontOfSize:22.0];
NSString * aText = @"Hello";
[aText drawAtPoint:CGPointMake(220, 370) withFont: textFont];
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * imageData = UIImagePNGRepresentation(self.image);
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Image.png"];
NSLog(@"filePath :%@", filePath);
BOOL isFileCreated = [fm createFileAtPath:filePath contents:imageData attributes:nil];
if(isFileCreated)
{
NSLog(@"File created at Path %@",filePath);
}
}
这是我用来画相同图像的代码,除了我没有使用自然图像。它不在draw rect中,但根据您的最终目标,最好在自定义方法中的draw rect之外执行此操作,并将self.image设置为-UIImage*imageToDraw的结果。它的输出是: 代码如下:
- (UIImage *)imageToDraw
{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 480), NO, [UIScreen mainScreen].scale);
UIImage *natureImage = [UIImage imageNamed:@"testImage.png"];
[natureImage drawInRect:CGRectMake(130, 380, 50, 50)];
UIFont *numberFont = [UIFont systemFontOfSize:28.0];
NSString * aNumber = @"111";
[aNumber drawAtPoint:CGPointMake(100, 335) withFont:numberFont];
UIFont *textFont = [UIFont systemFontOfSize:22.0];
NSString * aText = @"Hello";
[aText drawAtPoint:CGPointMake(220, 370) withFont:textFont];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
- (NSString *)filePath
{
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"Image.png"];
}
- (void)testImageWrite
{
NSData *imageData = UIImagePNGRepresentation([self imageToDraw]);
NSError *writeError = nil;
BOOL success = [imageData writeToFile:[self filePath] options:0 error:&writeError];
if (!success || writeError != nil)
{
NSLog(@"Error Writing: %@",writeError.description);
}
}
无论如何-希望这有帮助您的图像参考系是标准的iOS参考系:原点在左上角。相反,使用核心文本绘制的文本具有旧的参照系,原点位于左下角。只需对文本而不是图形上下文应用变换,如下所示:
CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0f, -1.0f));
然后,所有的东西都将被布局,就好像轴原点在左上角一样
如果你必须用核心文本而不是单词来写完整的句子,请看一下我想说的是,drawRect方法用于在视图中绘制,而不是创建图像。性能问题,drawInRect被称为很多。出于这个目的,最好使用viewDidLoad或一些自定义方法 我已经就您的请求做了一个示例:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat margin = 10;
CGFloat y = self.view.bounds.size.height * 0.5;
CGFloat x = margin;
UIImage *natureImage = [UIImage imageNamed:@"image"];
CGRect natureImageRect = CGRectMake(x, y - 20, 40, 40);
[natureImage drawInRect:natureImageRect];
x += 40 + margin;
UIFont *textFont = [UIFont systemFontOfSize:22.0];
NSString * aText = @"Hello";
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.alignment = NSTextAlignmentCenter;
NSDictionary *attr = @{NSParagraphStyleAttributeName: style,
NSFontAttributeName: textFont};
CGSize size = [aText sizeWithAttributes:attr];
[aText drawInRect: CGRectMake(x, y - size.height * 0.5, 100, 40)
withFont: textFont
lineBreakMode: UILineBreakModeClip
alignment: UITextAlignmentLeft];
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.imageView.image = self.image;
}
如何应用CGContextSetTextMatrixcontext,CGAffineTransformMakeScale1.0f,-1.0f;只用于文本?