Saving photo with added overlay to photo library(将添加了叠加层的照片保存到照片库)
问题描述
我正在制作一个应用程序,用户在其中拍照,在图像上放置一个叠加层,然后用户可以保存图像或将其上传到 Facebook 或其他网站.
I'm making an app where the user takes a photo, an overlay is placed over the image, and the user can then save the image or upload it to Facebook or other sites.
我已经设法让应用程序拍摄照片,并使用放置在照片顶部的 UIImageView
来制作叠加层.
I have managed to get the app to take the photo, and to make the overlay I am using a UIImageView
, which is placed over the top of the photo.
我不确定如何将带有叠加层的图像作为一个文件导出到照片库或 Facebook.
I'm not sure how exactly I can export the image, with the overlay, as one file, to the photo library, or to Facebook.
推荐答案
这应该给你的想法.不过,此代码可能需要一两次调整.(我承认我没有运行它.)
This should give you the idea. This code may need a tweak or two, though. (I admit that I haven't run it.)
UIImageView
的基础层可以将自己渲染到 CoreGraphics 位图上下文中(当然,这将包括所有子层),然后可以为您提供新的 UIImage代码>.
Your UIImageView
's base layer can render itself into a CoreGraphics bitmap context (this will include all the sublayers, of course), which can then provide you with a new 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 编程指南的 "创建位图图形上下文".
There's also a slightly more complicated way that gives you more control over things like color, which involves creating a CGBitmapContext
and getting a CGImageRef
, from which you can again produce a UIImage
. That is described in the Quartz 2D Programming Guide, section "Creating a Bitmap Graphics Context".
这篇关于将添加了叠加层的照片保存到照片库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将添加了叠加层的照片保存到照片库
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01