How to convert Text to Image in Cocoa Objective-C -
I am looking for a way to convert a text of coca into an image. Everything does not describe an image converting text into text into text.
Simply put, I want to take a word (such as "Kevin") and convert it into a bitmap image and save it as a JPEG.
People who answer are awesome, thanks for doing this to three different and equally valid ways (yes I have tested them) .... very good, I think I You can answer all right.
Edit: I forgot the question and assumed you want coco-touch code (I Except it is left) This is one way of using the Cortex in cocoa (as another poster says there are ways):
{NSString * string = @ "Kevin"; CGFloat fontSize = 12.0f; // Create a string with string and font information CTFontRef font = CTFontCreateWithName (CFRCR ("Helvetica Lite"), fontSize, Zero); NSDictionary * attributes = [Object Endase with NSDictionary dictionary: (id) font, kCTFontAttributeName, zero]; NSAttributedString * as = [[NSAttributedString alloc] initWithString: string attributes: attributes]; CFRelease (font); // How big is the image that needs the CTLineRef line / CTLIEETEATED string (like CFATADRIFRF); CGFlot climbing, lineage, pioneer; Double f width = CTLineGetTypographicBounds (line, and ascendant, and lineage, and leading); // On iOS 4.0 and Mac OS X v10.6, you can pass the data size_t width = (size_t) to blank (fWidth) blank; Size_t height = (size_t) CEF (climbing + offspring); Zero * Data = Molok (width * height * 4); // References and fill it with a white background CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB (); CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast; CGContextRef ctx = CGBitmapContextCreate (Data, width, height, 8, width * 4, location, bitmapinfo); CGColorSpaceRelease (space); CGContextSetRGBFillColor (CTX, 1.0, 1.0, 1.0, 1.0); // white background CGContextFillRect (ctx, CGRectMake (0.0, 0.0, width, height)); // Text CGFlot X = 0.0 Draw; CGFloat y = lineage; CGContextSetTextPosition (CTX, X, Y); CTLineDraw (line, CTX); CFRelease (line); // Save as JPEG CGImageRef imageRef / CGBitmapContextCreateImage (ctx); NSBitmapImageRep * imageRep = [[NSBitmapImageRep alloc] initWithCGImage: imageRef]; NSAssert (imageRep, @ "imageRep should not be zero"); NSData * imageData = [imageRep by typing Type: NSJPEGFileType properties: zero]; NSString * filename = [NSString stringWithFormat: @ "Kevin.jpg"]; NSString * file directory = NSHome directory (); NSString * filePath = [File directory stringbapping pathCompany: filename]; [ImageData writeToFile: File path atomically: Yes]; // clear [imageRep release]; CGImageRelease (imageRef); Free (data); } This is a coco-touch version:
// Understand the dimensions of the string in a given font. NSString * Kevin = @ "Kevin"; UIFont * font = [UIFont systemFontOfSize: 12.0f]; CGSize size = [Kevin Size WithFont: font]; // Create a bitmap context in which text will be provided. UIGraphicsBeginImageContext (size); // Render the text [Kevin Drawpoint: CGPindem (0.0, 0.0) fonts: fonts]; // retrieve image UIImage * image = UIGraphicsGetImageFromCurrentImageContext (); // JPEG Change in NSData * Data = UIImageJPEG Report (Image, 1.0); // Understand a safe path NSArray * arrayPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * docDir = [ArrayPath ObjectAntIndex: 0]; // Write the file NSString * filePath = [docDir stringByAppendingPathComponent: @ "Kevin.jpg"]; BOOL success = [data typefile: filepath atom: yes]; If (! Success) {fails to write in file "NSLog (" maybe it already exists? "); } And {nslog (@ "jpeg file successfully written at% @", filepath); } // clear UIGraphicsEndImageContext (); When I started programming iOS, I found the following things as unwanted or unusual. Methods to measure and attract strings on NSString (Graphics Reference as Other System) No way to save data NSDTa not a method on a file class is! To create graphics references, the functions are simple functions and are not part of any class. Hope it helps!
Comments
Post a Comment