I think you need to add the your UIImageView and your UILabel as a subview of a particular UIView (Suppose we call this view as customView
) and then take the screenshot of the view using
how to take a screenshot of the iPhone programmatically?
Answer from the link modified to make it more easy for your understanding which is as follows:
Important: Add QuartzCore Framework
and add #import<QuartzCore/QuartzCore.h>
CGSize screenSize = [[UIScreen mainScreen] applicationFrame].size;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(nil, screenSize.width, screenSize.height, 8, 4*(int)screenSize.width, colorSpaceRef,
kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(ctx, 0.0, screenSize.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
[(CALayer*)customView.layer renderInContext:ctx];
CGImageRef cgImage = CGBitmapContextCreateImage(ctx);
UIImage *image = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
CGContextRelease(ctx);
[UIImageJPEGRepresentation(image, 1.0) writeToFile:filePath atomically:NO];
Here filePath is your application's Documents directory file path.
You can get your NSDocuments Directory file path using:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
Now execute this code on any button or other control click you want the view to be captured.
Let me know if you need more help.
Hope this helps you.