我正在制作一个应用,用户可以在其中拍照,在图像上放置一个叠加层,然后用户可以保存图像或将其上传到 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/