如何将2个UIImageView中的2个图像合并为1张图像



我想将2个图像合并为2 uiimageview中的1个图像,与我在屏幕上看到的"正确"位置,角度,比例比例。

在我的项目中,有2个UIimageView - 图像视图是主要的UIImageView - TopimageView是可以拖动,旋转,比例

的覆盖层UIImageView

我可以通过绘制2张图像,但位置错误,角度比例刻度保存图像。

这是我的代码:

UIImage *img = topImageView.image;
UIImage *bottomImg = self.imageView.image;
CGSize bounds = self.imageView.image.size;
UIGraphicsBeginImageContext(bounds);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, topImageView.image.size.width * 0.5, topImageView.image.size.height * 0.5);
CGFloat angle = atan2(topImageView.transform.b, topImageView.transform.a);
CGContextRotateCTM(ctx, angle);
[topImageView.image drawInRect:CGRectMake(-topImageView.image.size.width * 0.5,
                                          -topImageView.image.size.height * 0.5,
                                          topImageView.image.size.width,
                                          topImageView.image.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContext(bounds);
[bottomImg drawInRect:CGRectMake(0, 0, bounds.width, bounds.height)];
[newImage drawInRect:CGRectMake(topImageView.frame.origin.x,
                                topImageView.frame.origin.y,
                                newImage.size.width,
                                newImage.size.height)];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

有帮助我的想法吗?非常感谢!

将两个图像作为子视图添加到uiview。做您需要的图像,然后拍摄uiview的快照:

UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *MergedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

尝试此

#pragma mark - Marge two Images 
- (UIImage *) addImageToImage:(UIImage *)img withImage2:(UIImage *)img2{
    CGSize size = CGSizeMake(imageView.image.size.width, imageView.image.size.height);
    UIGraphicsBeginImageContext(size);
    CGPoint pointImg1 = CGPointMake(0,0);
    [img drawAtPoint:pointImg1]; 
    CGPoint pointImg2 = CGPointMake(0,0);
    [img2 drawAtPoint: pointImg2];
    UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return result;
}

[self addImageToImage:imageView.image withImage2:imageView2.image];

相关内容

  • 没有找到相关文章

最新更新