Iphone – Create a UIImage from two other UIImages on the iPhone

iphoneuiimage

I'm trying to write an animation on the iPhone, without much success, getting crashes and nothing seems to work.

What I wanna do appears simple, create a UIImage, and draw part of another UIImage into it, I got a bit confused with the context and layers and stuff.

Could someone please explain how to write something like that (efficiently), with example code?

Best Answer

For the record, this turns out to be fairly straightforward - everything you need to know is somewhere in the example below:

+ (UIImage*) addStarToThumb:(UIImage*)thumb
{
   CGSize size = CGSizeMake(50, 50);
   UIGraphicsBeginImageContext(size);

   CGPoint thumbPoint = CGPointMake(0, 25 - thumb.size.height / 2);
   [thumb drawAtPoint:thumbPoint];

   UIImage* starred = [UIImage imageNamed:@"starred.png"];

   CGPoint starredPoint = CGPointMake(0, 0);
   [starred drawAtPoint:starredPoint];

   UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   return result;
}