gpt4 book ai didi

iphone - layer.renderInContext 没有考虑 layer.mask 吗?

转载 作者:可可西里 更新时间:2023-11-01 03:58:36 25 4
gpt4 key购买 nike

我正在尝试将一些 UIImages 渲染成一张我可以保存在我的相册中的图像。但是好像 layer.renderInContext 没有考虑图层蒙版?

当前行为:照片保存,我看到了 mosaicLayer,没有 maskLayer 的 mask 效果。

预期行为:照片保存,我在我的 View 中看到图像,上面有一个蒙版 mosaicLayer。

我使用下面的代码来屏蔽图像

UIImage *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];

然后我使用这段代码来保存我合成的图像:

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 switch
CGContextTranslateCTM(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 switch
CGAffineTransform 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 在 Mac OS X 10.5 上不受支持。我怀疑它在 iOS 上是一样的,即使文档没有这么说。

要解决您的问题,您可能必须将图层分别绘制到图形上下文中,并在图形上下文本身上设置正确的 mask 。

关于iphone - layer.renderInContext 没有考虑 layer.mask 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4896296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com