将照片添加到照片库中保存照片

时间:2011-03-19 03:33:24

标签: objective-c cocoa-touch ios uiimageview

我正在创建一个应用程序,用户拍照,在图像上放置一个叠加层,然后用户可以保存图像或将其上传到Facebook或其他网站。

我设法让应用程序拍摄照片,并制作叠加层我使用的是UIImageView,它位于照片的顶部。

我不确定我是如何将图像,叠加层,作为一个文件导出到照片库或Facebook。

1 个答案:

答案 0 :(得分:2)

这应该会给你一个想法。但是,此代码可能需要一两个调整。 (我承认我没有运行它。)

您的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"部分对此进行了描述。

相关问题