将两个图像合并在一起并保存第二个图像的特定位置

时间:2013-03-29 12:32:30

标签: iphone ios objective-c

我正在尝试允许用户将预先选择的图像拖动并定位到他们从库或照片库中选择的图像,并整体保存新编辑的图像。

下面的代码会合并两个图像,但不会将第二个图像保存在用户选择的相同位置。 “userImage”是非静态的。再次感谢您的任何意见!

//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 个答案:

答案 0 :(得分:0)

你在说:

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

嗯,显然这意味着userImage将在0,0点(左上角)绘制。如果您不想在那里绘制图像,请不要在那里绘制!把它画到你想要的地方。

顺便说一句,如果您要绘制一个自己大小的整个图像,则不需要使用drawInRect:;只需使用drawAtPoint:。它更简单。