Merging two images of different size with rotate a

2019-06-05 15:23发布

问题:

I'm facing the following problem : I have to merge two images bottomImg and topImg to create a new image C as a result of the merging. I already know how to merge two images but in this case my goal is a little bit different. top image may be zoomed and rotated.

I have follow this answer. But in this solution top image is cut while i rotate. simulator

actual stored image is document like

I have try this code

_parentViewGIf for bottomImg and for _ivGIF for 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;
}

Thanks in advance for any help or suggestion