从 UIView 层次结构创建 UIImage 时背景透明度丢失
Background transparency is lost when creating a UIImage from a UIView hierarchy
我正在开发一款可以让用户将图像放入相框中的应用程序。然后,他们可以使用 iOS 共享 sheet 以某种方式共享该图像。我遇到的问题是框架有圆角,但是当我通过将图像保存到相机胶卷进行测试时,背景是白色的,不透明,我不知道如何用透明的方式保存它背景.
这是我的代码:
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];
这是我的视图层次结构,来自 Xcode 的视图调试器:
上面代码中突出显示的UIView是self.framedImage
。 UIImageView 和包含 UIImageView 的 UIButton 都是保存的层次结构的一部分。
我知道在 opaque
标志设置为 NO 的情况下使用 UIGraphicsBeginImageContextWithOptions
意味着 my image must have an alpha channel,但据我所知,确实如此。 UIView、UIImageView 和 UIButton 都有 [UIColor clearColor]
作为它们的背景颜色,并且对于所有这三个,Interface Builder 中的 'Opaque' 复选框都未选中。当我检查调试器中的视图时,它们的背景颜色的 alpha 值为 0。
我已经尝试在代码中将背景显式设置为 [UIColor clearColor]
,就在上面的代码片段之前,但它仍然以白色背景保存。我尝试将背景更改为另一种颜色(紫色),并按照我期望的那样保存为紫色背景。所以问题似乎是 UIView 的透明区域在 UIImage 中被转换为不透明的白色。
有人知道我在这里遗漏了什么吗?
多亏了 dasblinkenlight 的评论,我才找到了问题所在。
我将 UIImage 传递给 UIActivityViewController,以便呈现系统共享 sheet。但是当我按下 "Save Image" 按钮将图像保存到相机胶卷时,默认情况下它保存为不支持透明度的 jpeg。在将图像传递给 UIActivityViewController 之前将图像转换为 PNG 解决了这个问题。
固定代码如下:
UIGraphicsBeginImageContextWithOptions(self.framedImage.bounds.size, NO, 0.0f);
[self.framedImage drawViewHierarchyInRect:self.framedImage.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//convert to PNG
NSData *pngImageData = UIImagePNGRepresentation(image);
//present the system share sheet for the PNG data
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[pngImageData] applicationActivities:nil];
我正在开发一款可以让用户将图像放入相框中的应用程序。然后,他们可以使用 iOS 共享 sheet 以某种方式共享该图像。我遇到的问题是框架有圆角,但是当我通过将图像保存到相机胶卷进行测试时,背景是白色的,不透明,我不知道如何用透明的方式保存它背景.
这是我的代码:
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];
这是我的视图层次结构,来自 Xcode 的视图调试器:
上面代码中突出显示的UIView是self.framedImage
。 UIImageView 和包含 UIImageView 的 UIButton 都是保存的层次结构的一部分。
我知道在 opaque
标志设置为 NO 的情况下使用 UIGraphicsBeginImageContextWithOptions
意味着 my image must have an alpha channel,但据我所知,确实如此。 UIView、UIImageView 和 UIButton 都有 [UIColor clearColor]
作为它们的背景颜色,并且对于所有这三个,Interface Builder 中的 'Opaque' 复选框都未选中。当我检查调试器中的视图时,它们的背景颜色的 alpha 值为 0。
我已经尝试在代码中将背景显式设置为 [UIColor clearColor]
,就在上面的代码片段之前,但它仍然以白色背景保存。我尝试将背景更改为另一种颜色(紫色),并按照我期望的那样保存为紫色背景。所以问题似乎是 UIView 的透明区域在 UIImage 中被转换为不透明的白色。
有人知道我在这里遗漏了什么吗?
多亏了 dasblinkenlight 的评论,我才找到了问题所在。
我将 UIImage 传递给 UIActivityViewController,以便呈现系统共享 sheet。但是当我按下 "Save Image" 按钮将图像保存到相机胶卷时,默认情况下它保存为不支持透明度的 jpeg。在将图像传递给 UIActivityViewController 之前将图像转换为 PNG 解决了这个问题。
固定代码如下:
UIGraphicsBeginImageContextWithOptions(self.framedImage.bounds.size, NO, 0.0f);
[self.framedImage drawViewHierarchyInRect:self.framedImage.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//convert to PNG
NSData *pngImageData = UIImagePNGRepresentation(image);
//present the system share sheet for the PNG data
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[pngImageData] applicationActivities:nil];