Saving photo with added overlay to photo library
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.
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.
I'm not sure how exactly I c开发者_StackOverflow中文版an 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.)
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();
These functions are all described in the UIKit Function Reference.
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".
精彩评论