如何使用透明笔触保存图像 [英] How to save image with transparent stroke

查看:28
本文介绍了如何使用透明笔触保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照@Rob 的回答并按照我的意愿绘制了...但是当我保存此图像时....笔触不再透明

I followed @Rob answer and its drawn as I want...but when I saved this image....stroke not transparent anymore

Objective-C 如何避免在具有 UIBezierPath 笔画颜色的同色图像上绘制

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

For save image I written this code

 -(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);
}

推荐答案

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

You are either setting the alpha of the image view with the paths to 1.0 somewhere, or you are using something that doesn't permit transparencies (e.g. UIGraphicsBeginImageContextWithOptions with YES for the opaque parameter, or staging the image in a format that doesn't preserve alpha, such as JPEG).

一些额外的想法:

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

  1. I notice that you're only drawing upperImageView. If you want the composite image, you need to draw both. Or are you only trying to save one of the image views?

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

(For those unfamiliar with that other question, the entire point was how to draw multiple paths over an image, and have the full set of those paths with the same reduced alpha, rather than having the intersection of paths lead to some additive behavior. This was accomplished by having two separate image views, one for the underlying image, and one for the drawn paths. The key to the answer to that question was that one should draw the paths at 100% alpha, but to add that as a layer to a view that, itself, has a reduced alpha.)

你正在绘制的图像视图的 alpha 是什么.

What is the alpha of the image view upon which you are drawing.

注意:在另一个问题的答案中,当保存组合路径的临时副本时.我们不得不暂时将 alpha 设置为 1.0.但是在此处保存最终结果时,我们希望保留路径".降低后的图像视图的 alpha.

NB: In the answer to that other question, when saving a temporary copy of the combined paths. we had to temporarily set the alpha to 1.0. But when saving the final result here, we want to keep the "path" image view's alpha at its reduced value.

不相关,但您忠实地抄录了我最初示例中的一个错字(因为已修复),在该示例中我不小心调用了 UIGraphicsEndPDFContext 而不是 UIGraphicsEndImageContext.你应该使用后者.

Unrelated, but you faithfully transcribed a typo (since fixed) in my original example where I accidentally called UIGraphicsEndPDFContext rather than UIGraphicsEndImageContext. You should use the latter.

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

So, considering two image views, one with the photograph and one with the drawn path, called imageImageView (with alpha of 1.0) and pathsImageView (with alpha of 0.5), respectively, I can save the snapshot like so:

- (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);
}

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

When I did that, the resulting composite image was in my photo album:

这篇关于如何使用透明笔触保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆