在 UIImage 之上添加图像
我试图在保存之前在 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;
}
I am trying to add a image banner on top of a UIImage
before saving it. I have created this category method to do so. I have checked and rechecked that banner.png
exists and the gradient
shows up fine.
What am I doing wrong?
Thanks in advance!
- (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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显然
[UIImage imageWithContentsOfFile:]
需要完整路径。将其更改为[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"banner.png"]]
有效。Apparently
[UIImage imageWithContentsOfFile:]
requires a full path. Changing it to[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"banner.png"]]
worked.请参考使用UIImage & 组合图像CGContext –(离屏绘图)
Please refer Combining Images with UIImage & CGContext – (Offscreen drawing)