gpt4 book ai didi

ios - 如何用透明描边保存图像

转载 作者:行者123 更新时间:2023-11-28 21:50:58 25 4
gpt4 key购买 nike

我按照@Rob 的回答按照我的意愿绘制了它......但是当我保存这张图片时......笔划不再透明

Objective-C How to avoid drawing on same color image having stroke color of UIBezierPath

为了保存图片我写了这段代码

enter image description here

 -(void)saveImage {
UIGraphicsBeginImageContextWithOptions(self.upperImageView.bounds.size, NO, 0);

if ([self.upperImageView respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
[self.upperImageView drawViewHierarchyInRect:self.upperImageView.bounds afterScreenUpdates:YES]; // iOS7+
else
[self.upperImageView.layer renderInContext:UIGraphicsGetCurrentContext()]; // pre iOS7
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndPDFContext();

UIImageWriteToSavedPhotosAlbum(self.upperImageView.image, self,@selector(image:didFinishSavingWithError:contextInfo:), nil);
}

最佳答案

您要么将带有路径的 ImageView 的 alpha 设置为某处的 1.0,要么您正在使用不允许透明的东西(例如 UIGraphicsBeginImageContextWithOptionsYES 用于不透明参数,或以不保留 alpha 的格式暂存图像,例如 JPEG)。

一些额外的想法:

  1. 我注意到您只绘制了 upperImageView。如果您想要合成图像,则需要同时绘制两者。或者您只是想保存其中一个 ImageView ?

    (对于那些不熟悉其他问题的人,重点是如何在图像上绘制多条路径,并使这些路径的完整集合具有相同的减少 alpha,而不是让路径的交集导致一些附加的行为。这是通过两个单独的 ImageView 实现的,一个用于底层图像,一个用于绘制路径。回答该问题的关键是应该以 100% alpha 绘制路径,但将其添加为 View 的一个层,它本身具有减少的 alpha。)

  2. 您正在绘制的 ImageView 的 alpha 是多少。

    注意:在另一个问题的答案中,保存组合路径的临时副本时。我们不得不暂时将 alpha 设置为 1.0。但是当在这里保存最终结果时,我们希望将“路径” ImageView 的 alpha 保持在其降低的值。

  3. 不相关,但您在我的原始示例中忠实地记录了一个拼写错误(已修复),在该示例中我不小心调用了 UIGraphicsEndPDFContext 而不是 UIGraphicsEndImageContext。您应该使用后者。

因此,考虑两个 ImageView ,一个是照片,一个是绘制的路径,称为 imageImageView(alpha 为 1.0)和 pathsImageView(alpha 为 0.5),我可以像这样保存快照:

- (void)saveSnapshot {
CGRect rect = self.pathsImageView.bounds;

UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
if ([self.pathsImageView respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
[self.imageImageView drawViewHierarchyInRect:rect afterScreenUpdates:YES]; // iOS7+
[self.pathsImageView drawViewHierarchyInRect:rect afterScreenUpdates:YES];
} else {
[self.imageImageView.layer renderInContext:UIGraphicsGetCurrentContext()]; // pre iOS7
[self.pathsImageView.layer renderInContext:UIGraphicsGetCurrentContext()];
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

当我这样做时,生成的合成图像在我的相册中:

image in photos album

关于ios - 如何用透明描边保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28358447/

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