将带有添加的叠加层的照片保存到照片库 [英] 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".
这篇关于将带有添加的叠加层的照片保存到照片库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!