如何在iOS中将重叠贴纸与图像合并

时间:2015-09-01 09:02:45

标签: ios iphone merge uiimageview uiimage

我正在尝试将单个图像中的两个不同图像合并,并尝试将其保存在照片库中。但是我试图在图像上合并的贴纸正在拉伸,并且没有正确对齐图像中的x和y。

我使用的代码如下:

- (UIImage*)buildImage:(UIImage*)image
{
    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);

    [image drawAtPoint:CGPointZero];

    CGFloat scale = image.size.width / _workingView.width;
    NSLog(@"%f",scale);
    CGContextScaleCTM(UIGraphicsGetCurrentContext(), scale, scale);
    [_workingView.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *tmp = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return tmp;
}

这里的代码_workingview是包含其中的贴纸图像的视图&争论中的图像是我们在函数中传递的主要图像

as you can see the image width is replace with image height

我非常感谢你的帮助

谢谢大家

1 个答案:

答案 0 :(得分:0)

CGFloat宽度,高度;

CGFloat width, height;
UIImage *inputImage;    // input image to be composited over new image as example

// create a new bitmap image context at the device resolution (retina/non-retina)
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), YES, 0.0);        

// get context
CGContextRef context = UIGraphicsGetCurrentContext();       

// push context to make it current 
// (need to do this manually because we are not drawing in a UIView)
UIGraphicsPushContext(context);                             

// drawing code comes here- look at CGContext reference
// for available operations
// this example draws the inputImage into the context
[inputImage drawInRect:CGRectMake(0, 0, width, height)];
 [stickerImage drawInRect:CGRectMake(yourX,yourY,othertWidth,otherHeight)]

// pop context 
UIGraphicsPopContext();                             

// get a UIImage from the image context- enjoy!!!
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();

// clean up drawing environment
UIGraphicsEndImageContext();