【问题标题】:Add image on top of UIImage在 UIImage 上添加图像
【发布时间】:2011-10-08 03:53:02
【问题描述】:

我正在尝试在保存之前在 UIImage 顶部添加图像横幅。我已经创建了这个类别方法来做到这一点。我已经检查并重新检查了banner.png 存在并且gradient 显示正常。

我做错了什么?

提前致谢!

- (UIImage *)addBanner {
     UIGraphicsBeginImageContext(CGSizeMake(self.size.width, self.size.height+50));
     [self drawAtPoint:CGPointMake(0, 50)];
     CGContextRef context = UIGraphicsGetCurrentContext();
     CGRect bannerRect = CGRectMake(0, 0, self.size.width, 50);
     CGColorRef start = RGB(71, 174, 255).CGColor;
     CGColorRef end = RGB(0, 80, 255).CGColor;
     drawLinearGradient(context, bannerRect, start, end);
     UIImage *bannerImage = [UIImage imageWithContentsOfFile:@"banner.png"];
     [bannerImage drawAtPoint:CGPointMake(0, 0)];
     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
     return newImage;
}

【问题讨论】:

  • 运行这段代码会发生什么?
  • 我的图像顶部带有横幅渐变,但没有显示横幅图像。
  • 原来 bannerImage 是 nil ,所以这导致了我的问题。我绝对肯定它在应用程序包中。

标签: iphone ios uiimage core-graphics


【解决方案1】:

显然[UIImage imageWithContentsOfFile:] 需要完整路径。将其更改为 [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"banner.png"]] 有效。

【讨论】:

  • 或者,取决于您将如何使用图像,[UIImage imageNamed:@"banner"]。详情请见the docs
【解决方案2】:

【讨论】:

  • 如果我明白你在说什么,我试图在我的类别之外运行它并得到相同的结果。在提供千篇一律的响应之前,请阅读问题描述。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多