views:

224

answers:

1

I am trying to save more then 1 image with a background image into a single image and then saving it out. I found out how to flatten the images, but the images I added on top of the background are not appearing in the correct position when flattening them. It seems like they appearing in different position with different background image sizes.

UIGraphicsBeginImageContext(backGroundImage.image.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
//CGRect area= [self bounds];
CGRect area = CGRectMake(0, 0, backGroundImage.image.size.width, backGroundImage.image.size.height);
CGRect area2= CGRectMake(testImage.frame.origin.x, testImage.frame.origin.y, testImage.frame.size.width, testImage.frame.size.height);
//Need to flip images to the correct orientation
 CGContextTranslateCTM(ctx, 0, area.size.height);
CGContextScaleCTM(ctx, 1, -1);
CGContextSetBlendMode(ctx, kCGBlendModeNormal);

//Drawing background first
CGContextDrawImage(ctx, area, backGroundImage.image.CGImage);

//Draw other images on top
//[[AccessoryManager sharedManager] SaveImages:ctx];
CGContextDrawImage(ctx, area2, testImage.image.CGImage);

//Creating one single image from context.
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum( newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil );
+1  A: 

The area2 looks quite suspicious and you might want to check if the numbers were right. Also, I saw frame property there. Are you mixing UIImageView and UIImage? You might want to try to use testImage.image's own size property.

lukhnos