Merging two images of different size with rotate and zoom

自古美人都是妖i 提交于 2019-12-23 03:27:28

问题


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

来源:https://stackoverflow.com/questions/38119172/merging-two-images-of-different-size-with-rotate-and-zoom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!