如何在iOS中将重叠的贴纸与图像合并
Posted
技术标签:
【中文标题】如何在iOS中将重叠的贴纸与图像合并【英文标题】:How merge the overlapping sticker with image in iOS 【发布时间】:2015-09-01 09:02:45 【问题描述】:我正在尝试将两个不同的图像合并为单个图像并尝试将其保存在照片库中。但是我试图在图像上合并的贴纸被拉伸并且没有正确对齐图像中的 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 是其中包含贴纸图像的视图,而争论中的图像是我们在函数中传递的主图像
非常感谢您的帮助
谢谢大家
【问题讨论】:
【参考方案1】:CGF浮动宽度、高度;
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();
【讨论】:
以上是关于如何在iOS中将重叠的贴纸与图像合并的主要内容,如果未能解决你的问题,请参考以下文章