2

I am developing an app in that a user can write a label(text) over the image and have to save it in iphone local storage with the label.

(i.e) User can add whatever text in the place provided over the image, and have to save it as image with the label he wrote in the iphone local storage

Thanks in advance

iPhone_suren
  • 181
  • 1
  • 3
  • 13

3 Answers3

1

you can add UILabel as a subview to the UIImageView contains the UIImage then

UIGraphicsBeginImageContextWithOptions(UIImageView.bounds.size, UIImageView.opaque, 0.0);
[UIImageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

you get the UIImage you want

or

just draw it yourself

UIGraphicsBeginImageContext(imageSize);   

......
CGContextDrawImage(.....);
CGContextShowTextAtPoint(.....);
......

UIGraphicsEndImageContext();   
adali
  • 5,977
  • 2
  • 33
  • 40
1

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.

Community
  • 1
  • 1
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
  • This code helps me a lot to capture the screen. can you please tell me how to capture the particular part of the screen – iPhone_suren Apr 19 '12 at 06:51
  • Add a view called customView to a particular part of the screen you are taking about, take the IBOutlet of the customView and then execute the code I have given in my answer. Refer to code given in my answer and not in the link, as the answer in the link is about whole screen capture :) Let me know if you need more help. – Parth Bhatt Apr 19 '12 at 07:03
  • Thanks Parth Bhatt. you made my work easier. – iPhone_suren Apr 19 '12 at 07:05
  • @iPhone_suren: Why you unaccepted my answer, after keeping it as accepted after 5-6 hours? You said it has made your work easier because of my answer. Whats the point? – Parth Bhatt Apr 19 '12 at 12:53
  • @iPhone_suren: I had already told you that you may have to make some changes in the frame to make a screenshot out of only required portion of the view. I think apart from it my code works fine. So I don't find a valid reason for you to unaccept my answer after 6 hours. – Parth Bhatt Apr 19 '12 at 12:55
0

You need to capture the screenshot of the screen as whole to have both in one image.

More info in Apple docs available here

Shubhank
  • 21,721
  • 8
  • 65
  • 83