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

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".

这篇关于将添加了叠加层的照片保存到照片库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将添加了叠加层的照片保存到照片库

基础教程推荐