问题
I have a app with 2 UIImageView that are Layed over each other. I have backgroundImg and FrontImg. Where frontImg can be: rotated, moved, scaled via the UIGestureRecognizers.
When I want to save my UIImages's I merge them, but they are saved as if they were never touched.
Does anyone knows how to fix this?
This is my saving method: UIGraphicsBeginImageContext(self.backgroundImg.image.size);
CGRect rect = CGRectMake(0, 0, self.backgroundImg.image.size.width, self.backgroundImg.image.size.height);
self.frontImg.contentMode = UIViewContentModeScaleAspectFit;
[self.backgroundImg.image drawInRect:rect];
[self.frontImg.image drawInRect:rect];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//[imageView3 setImage:resultingImage];
UIGraphicsBeginImageContextWithOptions(self.backgroundImg.bounds.size, NO,0.0);
[self.backgroundImg.image drawInRect:CGRectMake(0, 0, self.backgroundImg.frame.size.width, self.backgroundImg.frame.size.height)];
//UIImage *SaveImage = UIGraphicsGetImageFromCurrentImageContext();
//UIImage *resultingImage = [self mergeImage:self.backgroundImg.image withImage:self.frontImg.image];
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(resultingImage, self,@selector(image:didFinishSavingWithError:contextInfo:), nil);
These are my gesture functions:
-(IBAction)handlePan:(UIPanGestureRecognizer *)recognizer{
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
-(IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer
{
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale =1;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}
-(IBAction)handleRotate:(UIRotationGestureRecognizer *)recognizer
{
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
self.frontImg.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
recognizer.rotation = 0;
}
Hope someone can help me.
Gr. Leroy
回答1:
Your front image isn't being drawn at the coordinates, scale, etc it has in the view. You are drawing it with the same rect as the background (0, 0, backgroundWidth, backgroundHeight). You need to make sure that when you draw it to the graphics context you're getting the proper location, size, and scale.
If both of the images are contained within the same view, you can just screenshot that view. This post should help you out: http://ios-dev-blog.com/how-to-create-uiimage-from-uiview/
来源:https://stackoverflow.com/questions/13199319/how-to-save-uiimageview-after-uigesturerecognizer-have-passed