通过旋转和缩放合并两个不同大小的图像

时间:2016-06-30 09:34:33

标签: ios objective-c merge uiimage cgcontext

我面临以下问题:由于合并,我必须合并两个图像bottomImg和topImg以创建新图像C. 我已经知道如何合并两个图像但在这种情况下我的目标有点不同。 顶部图像可以缩放和旋转。

我关注this answer。但在此解决方案中,顶部图像在我旋转时被剪切。模拟器enter image description here

实际存储的图像是enter image description here

之类的文档

我试过这段代码

_parentViewGIf用于bottomImg,_ivGIF用于topImg

- (UIImage *)mergeImage:(UIImage *)bottomImg withImage:(UIImage *)topImg {


    float width=bottomImg.size.width*_ivGIF.frame.size.width/_parentViewGIf.frame.size.width;
    float height=bottomImg.size.width*_ivGIF.frame.size.height/_parentViewGIf.frame.size.width;

    UIImage * scaledTopImg = [self imageByScalingProportionallyWithImage:topImg ToSize:CGSizeMake(bottomImg.size.width, bottomImg.size.height)];

    UIGraphicsBeginImageContext(scaledTopImg.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(ctx, scaledTopImg.size.height * 0.5, scaledTopImg.size.height * 0.5);
    CGFloat angle = atan2(_ivGIF.transform.a, _ivGIF.transform.b);
    CGContextRotateCTM(ctx, angle);
    [scaledTopImg drawInRect:CGRectMake(- scaledTopImg.size.width * 0.5f, -(scaledTopImg.size.height  * 0.5f), scaledTopImg.size.width , scaledTopImg.size.height )];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    float x=bottomImg.size.width*_ivGIF.frame.origin.x/_parentViewGIf.frame.size.width;
    float y=bottomImg.size.width*_ivGIF.frame.origin.y/_parentViewGIf.frame.size.width;


    UIGraphicsBeginImageContext(bottomImg.size);
    [bottomImg drawInRect:CGRectMake(0, 0, bottomImg.size.width, bottomImg.size.height)];
    [newImage drawInRect:CGRectMake(0, 0, newImage.size.width, newImage.size.height)];
    UIImage *newImage2 = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage2;
}

提前感谢您的任何帮助或建议

0 个答案:

没有答案