如何通过UIGestureRecognizer之后如何保存UIImageView?

问题描述 投票:1回答:1

我有一个带有2个UIImageView的应用,它们相互放置。我有backgroundImg和FrontImg。可以在其中frontImg进行以下操作:通过UIGestureRecognizers进行旋转,移动,缩放。

当我要保存UIImage时,我将它们合并,但是它们被保存起来就好像从未被触摸过。

有人知道如何解决此问题吗?

这是我的保存方法: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);

这些是我的手势功能:

-(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;
}
ios xcode uiimage save
1个回答
0
投票

您的正面图像未按照视图中的坐标,比例等进行绘制。使用与背景相同的矩形(0、0,backgroundWidth,backgroundHeight)进行绘制。您需要确保在将其绘制到图形上下文时得到正确的位置,大小和比例。

如果两个图像都包含在同一视图中,则可以将该视图截屏。这篇文章应该可以帮助您:http://ios-dev-blog.com/how-to-create-uiimage-from-uiview/

© www.soinside.com 2019 - 2024. All rights reserved.