<分区>
我正在开发一款允许用户将图像放入框架中的应用程序。然后,他们可以使用 iOS 共享表以某种方式共享该图像。我遇到的问题是框架有圆角,但是当我通过将图像保存到相机胶卷进行测试时,背景是白色的,不透明,我不知道如何用透明的方式保存它背景。
这是我的代码:
UIGraphicsBeginImageContextWithOptions(self.framedImage.bounds.size, NO, 0.0f);
[self.framedImage drawViewHierarchyInRect:self.framedImage.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//present the system share sheet for the image
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[image] applicationActivities:nil];
这是我的 View 层次结构,来自 Xcode 的 View 调试器:
高亮显示的UIView就是上面代码中的self.framedImage
。 UIImageView 和包含 UIImageView 的 UIButton 都是保存的层次结构的一部分。
我了解使用 UIGraphicsBeginImageContextWithOptions
并将 opaque
标志设置为 NO 意味着 my image must have an alpha channel ,但据我所知,确实如此。 UIView、UIImageView 和 UIButton 都将 [UIColor clearColor]
作为它们的背景色,并且对于所有这三个,Interface Builder 中的“Opaque”复选框都未选中。当我在调试器中检查 View 时,它们的背景颜色的 alpha 值为 0。
我已经尝试在代码中将背景显式设置为 [UIColor clearColor]
,就在上面的代码片段之前,但它仍然以白色背景保存。我尝试将背景更改为另一种颜色(紫色),并按照我期望的那样保存为紫色背景。所以问题似乎是 UIView 的透明区域被转换为 UIImage 中的不透明白色。
有人知道我在这里缺少什么吗?