Saving a photo with overlay in the photo library

I create an application in which the user takes a photo, an inscription is placed above the image, and the user can then save the image or upload it to Facebook or other sites.

I managed to get the application to take a picture, and to create the overlay, I use UIImageView , which is placed on top of the photo.

I'm not sure how exactly I can export an image, overlaid as a single file, to a photo library or to Facebook.

+4
source share
1 answer

That should give you this idea. However, this code may need tweaking or two. (I admit that I did not run it.)

Your base level of UIImageView can be displayed in the context of a CoreGraphics bitmap (this, of course, will include all sublayers), 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(); 

All of these functions are described in the UIKit Feature Reference .

There is 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 create a UIImage again. This is described in the Quartz 2D Programming Guide, section β€œCreating a Graphic Raster Image Context” .

+2
source

Source: https://habr.com/ru/post/1344343/


All Articles