objective-c - 将添加了叠加层的照片保存到照片库

标签 objective-c cocoa-touch ios uiimageview

我正在制作一个应用,用户可以在其中拍照,在图像上放置一个叠加层,然后用户可以保存图像或将其上传到 Facebook 或其他网站。

我已经设法让应用程序拍摄照片,并使用 UIImageView 制作叠加层,它位于照片的顶部。

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

最佳答案

这应该会给您灵感。不过,此代码可能需要一两次调整。 (我承认我没跑过。)

您的 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 Function Reference 中描述。 .

还有一种稍微复杂的方法可以让您更好地控制诸如颜色之类的东西,它涉及创建一个 CGBitmapContext 并获取一个 CGImageRef,您可以从中再次生成一个 UIImage。这在 Quartz 2D 编程指南的 "Creating a Bitmap Graphics Context" 节中有描述。 .

关于objective-c - 将添加了叠加层的照片保存到照片库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5359868/

相关文章:

ios - 如何在搜索栏文本中显示?

objective-c - 库的所有代码都是用 Swift 编写的,但框架的 header 似乎是 Objective C?

ios - 在方法 objective-c 之外保留数组数据

objective-c - 为什么 ((self=[super init]]) 有效,但 (!(self=[super init])) 无效?

cocoa-touch - iPhone 应用程序退出时会调用什么方法?

ios - 错误 ITMS-90047 iTunes Connect

iphone - Obj-C,货币值(value)与可用数量,发行

ios - 将事件添加到以编程方式生成的 UIView

ios - 如何提高堆叠式 GPUImage 过滤器的性能

iphone - 编辑时更改 uitextfield 返回键类型