objective-c – 将照片添加到照片库中以保存照片

我正在创建一个应用程序,用户拍照,在图像上放置一个叠加,然后用户可以保存图像或将其上传到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”部分对此进行了描述.

点赞