在iOS中绘制加载的UIImage

时间:2013-02-05 18:14:38

标签: ios objective-c image uiimage draw

我有几个问题。

我有一个加载的UIImage,我想:

1st - 将另一张图片绘制到我加载的UIImage

第二 - 在我加载的UIImage上绘制一条线(颜色和粗细)

如果你想出一些基本的东西,我会非常感激,我还是一个菜鸟:)

1 个答案:

答案 0 :(得分:3)

还有另一种选择,它使用核心图形。

UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSaveGState(context);
UIImage *bottomImage = ...;
CGRect bottomImageRect = ...;
CGContextScaleCTM(context, 1.0, -1.0);
CGContextTranslateCTM(context, 0, -bottomImageRect.size.height);
CGContextDrawImage(context, bottomImageRect, bottomImage.CGImage);
CGContextRestoreGState(context);

CGContextSaveGState(context);
UIImage *topImage = ...;
CGRect topImageRect = ...;
CGContextScaleCTM(context, 1.0, -1.0);
CGContextTranslateCTM(context, 0, -topImageRect.size.height);
CGContextDrawImage(context, topImageRect, topImage.CGImage);
CGContextRestoreGState(context);

CGPoint origin = ...;
CGPoint end = ...;
CGContextMoveToPoint(context, origin.x, origin.y);
CGContextAddLineToPoint(context, end.x, end.y);
CGContextSetStrokeColorWithColor(context, [UIColor whatever].CGColor);
CGContextStrokePath(context);

UIImage *result = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

修改 稍微改变了代码,使图像不会反转