将添加叠加层的照片保存到照片库

4

我正在制作一款应用程序,用户可以拍摄照片,然后在图像上放置覆盖层,用户可以保存该图像或将其上传到Facebook或其他网站。

我已成功实现了拍照功能,并使用UIImageView制作了一个覆盖层,该覆盖层覆盖在照片的顶部。

我不确定如何将带有覆盖层的图像作为一个文件导出到照片库或Facebook等网站。


你有没有在这个问题上得到答案? - LilMoke
是的,请参见下面的答案。只需在创建的ImageView中绘制图像,然后您可以使用此方法从内容中创建UIImage。 - Nick Duffell
1个回答

2
这应该能让你有个想法。不过可能需要微调一下这段代码。(我承认我没有运行过。)
你的UIImageView的基础层可以将自己渲染到CoreGraphics位图上下文中(当然,这也包括所有的子层),然后提供给你一个新的UIImage
UIImage *finalImage;
// Get the layer
CALayer *layer = [myImageView layer];
// Create a bitmap context and make it the current context
UIGraphicsBeginImageContext(layer.bounds.size);
// Get a reference to the context
CGContextRef ctx = UIGraphicsGetCurrentContext();
// Ask the layer to draw itself
[layer drawInContext:ctx];
// Then ask the context itself for the image
finalImage = UIGraphicsGetImageFromCurrentImageContext();
// Finally, pop the context off the stack
UIGraphicsEndImageContext();

这些功能都在UIKit函数参考中进行了描述。
还有一种稍微复杂一些的方法,可以对颜色等方面进行更多的控制,涉及创建一个CGBitmapContext并获取CGImageRef,从而可以再次生成一个UIImage。该方法在Quartz 2D编程指南中有所描述,具体在“创建位图图形上下文”一节("Creating a Bitmap Graphics Context")。

1
请注意,如果您想绘制一个带有其层次结构的视图,则应使用CALayer类的renderInContext:方法。 - IlDan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接