本文介绍了layer.renderInContext不考虑layer.mask吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试将一些UIImages渲染成一张我可以保存在相册中的单张图像。但是看起来好像layer.renderInContext没有考虑图层蒙板?I'm trying to render some UIImages into 1 single image that I can save in my photo album. But it seems to as if the layer.renderInContext doesn't take a layermask into account? 当前行为:照片保存,我看看mosaicLayer,没有maskLayer的屏蔽效果。Current behavior: the photo saves, and I see mosaicLayer, without the masking effect of maskLayer. 预期的行为:照片保存,我在视图中看到了图片,顶部显示那个蒙面的mosaicLayer。Expected behavior: the photo saves and I see the image in my view, with on top of that a masked mosaicLayer.我使用下面的代码来掩盖图像I use the following code to mask the imageUIImage *maskImg = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"mask" ofType:@"png"]]; maskLayer = [[UIImageView alloc] initWithImage:maskImg]; maskLayer.multipleTouchEnabled = YES; maskLayer.userInteractionEnabled = YES; UIImageView *mosaicLayer = [[UIImageView alloc] initWithImage:img]; mosaicLayer.contentMode = UIViewContentModeScaleAspectFill; mosaicLayer.frame = [imageView bounds]; mosaicLayer.layer.mask = maskLayer.layer; [imageView addSubview:mosaicLayer];然后我使用此代码保存我的合成图像:And then i use this code to save my composed image:UIGraphicsBeginImageContext(imageView.bounds.size); [imageView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *saver = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageWriteToSavedPhotosAlbum(saver, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); 编辑:这会正确应用遮罩-(IBAction) saveImage {UIImage * saver = nil;CGImageRef image = imageView.image.CGImage;size_t cWidth = CGImageGetWidth(image);size_t cHeight = CGImageGetHeight(image);size_t bitsPerComponent = 8;size_t bytesPerRow = 4 * cWidth;//Now we build a Context with those dimensions.CGContextRef context = CGBitmapContextCreate(nil, cWidth, cHeight, bitsPerComponent, bytesPerRow, CGColorSpaceCreateDeviceRGB(), CGImageGetBitmapInfo(image));//The location where you draw your image on the context is not always the same location you have in your UIView,//this could change and you need to calculate that position according to the scale between you images real size, and the size of the UIImage being show on the UIView. Hence the mod floats...CGContextDrawImage(context, CGRectMake(0, 0, cWidth,cHeight), image);float mod = cWidth/(imageView.frame.size.width);float modTwo = cHeight/(imageView.frame.size.height);//Make the drawing right with coordinate switchCGContextTranslateCTM(context, 0, cHeight);CGContextScaleCTM(context, 1.0, -1.0);CGContextClipToMask(context, CGRectMake(maskLayer.frame.origin.x * mod, maskLayer.frame.origin.y * modTwo, maskLayer.frame.size.width * mod,maskLayer.frame.size.height * modTwo), maskLayer.image.CGImage);//Reverse the coordinate switchCGAffineTransform ctm = CGContextGetCTM(context);ctm = CGAffineTransformInvert(ctm);CGContextConcatCTM(context, ctm);CGContextDrawImage(context, CGRectMake(0, 0, cWidth,cHeight), mosaicLayer.image.CGImage);CGImageRef mergeResult = CGBitmapContextCreateImage(context);saver = [[UIImage alloc] initWithCGImage:mergeResult];CGContextRelease(context);CGImageRelease(mergeResult);UIImageWriteToSavedPhotosAlbum(saver, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);}推荐答案 renderInContext:的文档,其中包括 mask 。我怀疑它在iOS上是一样的,即使文档没有这么说。The documentation for renderInContext: that, among other properties, mask is not supported on Mac OS X 10.5. I suspect it is the same on iOS, even if the documentation doesn't say so.要解决您的问题,您可能需要将图层分别绘制到图形上下文,在图形上下文中设置正确的掩码。To solve your problem, you'll probably have to draw the layers separately into a graphics context, setting the correct mask on the graphics context itself. 这篇关于layer.renderInContext不考虑layer.mask吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-13 17:50