0

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;
}
rptwsthi
  • 10,094
  • 10
  • 68
  • 109
enbr
  • 1,203
  • 10
  • 16

2 Answers2

1

Apparently [UIImage imageWithContentsOfFile:] requires a full path. Changing it to [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"banner.png"]] worked.

enbr
  • 1,203
  • 10
  • 16
  • 1
    Or, depending on how you'll use the image, `[UIImage imageNamed:@"banner"]`. See [the docs](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/clm/UIImage/imageNamed:) for details. – Peter Hosey Oct 08 '11 at 16:55
0

Please refer Combining Images with UIImage & CGContext – (Offscreen drawing)

Dilip Rajkumar
  • 7,006
  • 6
  • 60
  • 76
  • If I understand what you're saying, I tried to run this outside of my category and got the same results. Please read the issue description before providing cookie cutter responses. – enbr Oct 08 '11 at 15:38