0

I'm trying to allow the user to drag and position pre-selected images onto a image that they choose from their library or photo gallery and save the new edited image, as a whole.

The code below gets the two images merged, but does not save the second one in the same spot that the user selected. The "userImage" is the one that is not static. Thanks again for any input!

//Saves the image to the camera roll
- (IBAction)savePhoto:(id)sender {


UIImage *backgroundImage = [imageView image]; //This image is static

//This image will be moved around by a touchesMoved event
UIImage *userImage = [UIImage imageNamed:@"chopper_stash.png"];

UIGraphicsBeginImageContext(backgroundImage.size);
[backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];

//I realize that this sends the image to the top left, but how do I declare it's new location
[userImage drawInRect:CGRectMake(0,0, userImage.size.width, userImage.size.height)];

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

//Save it to the camera roll
UIImageWriteToSavedPhotosAlbum(result, nil, nil, nil);
}

1 Answers1

0

You are saying:

[userImage drawInRect:CGRectMake(0,0, userImage.size.width, userImage.size.height)];

Well, obviously that means the userImage is going to be drawn at the point 0,0, which is the top left corner. If you do not want the image drawn there, do not draw it there! Draw it where you want it.

By the way, if you are drawing an entire image at its own size, you do not need to use drawInRect:; just use drawAtPoint:. It's simpler.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • No I'm sorry that I wasn't more clear. I get it that that will put the second image at the top left (that's not what I want though). The problems that I am having is locating the points of the "new location" of the second image. Sorry for any confusion, and thanks for the input. – user1504333 Mar 29 '13 at 13:17
  • Well, in your question you didn't show any code about how you are placing the `userImage`, so there's nothing more anyone can say. But obviously you will have to use MATH! Scary, eh? :) Basically you will have to figure out the position of second image view in terms of the bounds of the first image view. That's pretty easy (you will get a lot of help from `convertPoint:fromView:`), but the exact details will depend on how the image views treat their images (scaling, fitting, aspect, etc.). – matt Mar 29 '13 at 13:21
  • OK I'll look into that. I was just hoping that there was something like Paras mentioned above that would capture the new contents of the existing imageView as a whole. I was able to capture the new position with his reference above; however, it included all of my buttons too. Thanks again for your replies! – user1504333 Mar 29 '13 at 13:28
  • If it makes any difference, there is a comment above the userImage that says it's being placed by a touchesMoved event. – user1504333 Mar 29 '13 at 13:33