【问题标题】:Drawing into a loaded UIImage in iOS在 iOS 中绘制到加载的 UIImage
【发布时间】:2013-01-20 18:27:20
【问题描述】:

我有几个问题。

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

第一个 - 在我加载的 UIImage 上绘制另一个图像

第二次 - 在我加载的 UIImage 上画一条线(带有颜色和粗细)

如果你能提出一些基本的东西,我将不胜感激,我仍然是菜鸟:)

【问题讨论】:

  • 你的目标是什么?你想要一个包含所有这些绘图结果的新 UIImage 吗?或者您只是想在两个重叠的图像上显示一条线?

标签: ios objective-c image uiimage draw


【解决方案1】:

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

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();

编辑: 稍微更改了代码,以便图像不会倒置

【讨论】:

  • 感谢您的快速回答
  • 我忘了...图像是用倒置坐标系绘制的。检查修复的编辑
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-21
  • 2012-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多