gpt4 book ai didi

ios - 使用 Objective-C 合并两个 UIImageView,其中一个在使用转换后进行修改

转载 作者:行者123 更新时间:2023-12-01 16:28:08 24 4
gpt4 key购买 nike

我正在尝试创建一个 View Controller 来混合两个 UIImage。其中一个是静态的,作为背景的肌动蛋白,另一个可以缩放、拖动和旋转以将其放置在第一个内部我想要的位置。

我创建了一个这样的测试 View Controller :

enter image description here

蓝色背景( _backImageView )和马里奥图像( _marioImageView )都是同一级别的 UIImageViews(没有一个是另一个的子级)。我像这样处理所有手势识别器:

@interface ViewController () <UIGestureRecognizerDelegate>

@property (strong, nonatomic) IBOutlet UIPanGestureRecognizer *pan;
@property (strong, nonatomic) IBOutlet UIPinchGestureRecognizer *pinch;
@property (strong, nonatomic) IBOutlet UIRotationGestureRecognizer *rotation;

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

_pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[_marioImageView addGestureRecognizer:_pan];

_pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(resize:)];
[_marioImageView addGestureRecognizer:_pinch];

_rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[_marioImageView addGestureRecognizer:_rotation];
}



- (void)move:(UIPanGestureRecognizer *)pan
{
CGPoint translation = [pan translationInView:_backImageView];

CGPoint newCenter = CGPointMake(pan.view.center.x + translation.x,
pan.view.center.y + translation.y);

CGPoint newBottomRight = CGPointMake(newCenter.x + pan.view.frame.size.width / 2,
newCenter.y + pan.view.frame.size.height / 2);

CGPoint newOrigin = CGPointMake(newCenter.x - pan.view.frame.size.width / 2,
newCenter.y - pan.view.frame.size.height / 2);

if (CGRectContainsPoint(_backImageView.frame, newBottomRight) &&
CGRectContainsPoint(_backImageView.frame, newOrigin)) {

pan.view.center = newCenter;
[pan setTranslation:CGPointZero inView:_backImageView];
}
}

- (void)resize:(UIPinchGestureRecognizer *)pinch
{
if (CGRectContainsRect(_backImageView.frame, pinch.view.frame)) {
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
pinch.scale = 1.0;
}
}

- (void)rotate:(UIRotationGestureRecognizer *)rotation
{
if (CGRectContainsRect(_backImageView.frame, rotation.view.frame)) {
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
rotation.rotation = 0;
}
}

然后,当我触摸“混合图像”按钮时,它就是这样做的(
_finalImageView
是另一个 UIImageView,用于查看混合的结果图像。
- (IBAction)mixImages:(id)sender
{
UIImage *backImage = _backImageView.image;
UIImage *marioImage = _marioImageView.image;

CGSize finalSize = backImage.size;
CGSize marioSize = marioImage.size;

UIGraphicsBeginImageContext(finalSize);

[backImage drawInRect:CGRectMake(0, 0, finalSize.width, finalSize.height)];

CGPoint relativeOrigin = [_marioImageView convertPoint:_marioImageView.frame.origin toView:_backImageView];

[marioImage drawInRect:CGRectMake(relativeOrigin.x,
relativeOrigin.y,
marioSize.width,
marioSize.height)];

UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

[_finalImageView setImage:finalImage];
}

问题是,在最终图像中, 马里奥图像显示没有任何变换 (没有比例,没有旋转,任何东西)。

我做错了什么?

谢谢

最佳答案

当你这样做 imageView.image ,它只返回 image没有任何转变。您需要保存所有这些转换,并且您需要编写自己的代码,以从 ImageView 中提取转换后的图像。

一种方法是在旋转/缩放/变换后截取 imageview:

    //call this method after the rotation/scaling is done i.e in your mix method
-(UIImage *)screenshotImageView :(UIImageView *)imgV{

UIGraphicsBeginImageContext(imgV.frame.size);

CGContextRef c = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(c, imgV.frame.origin.x, imgV.frame.origin.y);

[self.view.layer renderInContext:c];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image;

}

因此,在这种情况下,您要合并的两个图像方法将是:
    UIImage *backImage = [self screenshotImageView:_backImageView];
UIImage *marioImage =[self screenshotImageView:_marioImageView];

使用返回的图像根据需要合并图像。

另一种方法是根据图像属性获取转换后的图像:

假设您在 imageView 中翻转了图像,如果您想获取翻转后的图像:

你可以试试:
     UIImage* flippedImage = [UIImage imageWithCGImage:imgView.image.CGImage
scale:imgView.image.scale
orientation:UIImageOrientationUpMirrored];

关于ios - 使用 Objective-C 合并两个 UIImageView,其中一个在使用转换后进行修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34136585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com