我正在制作一个应用程序,用户可以在其中拍照,在图像上放置叠加层,然后用户可以保存图像或将其上传到 Facebook 或其他网站。
我已经设法让应用程序拍照,并制作我正在使用的叠加层UIImageView
,它被放置在照片的顶部。
我不确定如何将带有叠加层的图像作为一个文件导出到照片库或 Facebook。
这应该会给你一个想法。不过,这段代码可能需要一两次调整。 (我承认我没有运行过。)
Your 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 函数参考 http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html.
还有一种稍微复杂的方法可以让您更好地控制颜色等内容,其中涉及创建一个CGBitmapContext
并得到一个CGImageRef
,从中您可以再次生成UIImage
。 Quartz 2D 编程指南 部分对此进行了描述“创建位图图形上下文” http://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html#//apple_ref/doc/uid/TP30001066-CH203-CJBHBFFE.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)