Reputation: 6152
I want am working on a project where I need to save user typed text as .PNG file through objective-c. I mean when user typed some text I give option to the user to choose font type then save it. After that the file will be saved as .PNG file without background.
How can I achieve this. Please share your views.
Thanks in advance
Upvotes: 2
Views: 1513
Reputation: 38239
use this method get image from text:
-(UIImage *)imageFromText:(NSString *)text
{
// set the font type and size
//UIFont *font = [UIFont systemFontOfSize:txtView.font];
CGSize size = [text sizeWithFont:txtView.font]; // label or textview
// check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+)
if (UIGraphicsBeginImageContextWithOptions != NULL)
UIGraphicsBeginImageContextWithOptions(size,NO,0.0);
else
// iOS is < 4.0
UIGraphicsBeginImageContext(size);
[text drawInRect:CGRectMake(0,0,size.width,size.height) withFont:txtView.font];
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return testImg;
}
Now u have image save like this
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath = [NSString stringWithFormat:@"%@/test.png",docDir]; // any name u want for image
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)]; your image here
[data1 writeToFile:pngFilePath atomically:YES];
Upvotes: 5