2009-10-30 14 views
6

पर टेक्स्ट जोड़ें छवि में टेक्स्ट कैसे जोड़ें और एक छवि के रूप में प्रदर्शित करें। आईएम फोटो लाइब्रेरी से छवि का चयन कर रहा है और फिर उस पर टेक्स्ट लिख रहा है और एक छवि के रूप में सहेजना चाहता है? तो क्या कोई यह जान सकता है कि इससे किससे निपटना है?UIImage

उत्तर

6

मैं UIImage करने के लिए एक पाठ जोड़ने के लिए निम्नलिखित कोड का उपयोग किया गया, उम्मीद है कि यह मदद मिलेगी

UIImage *img = [self drawText:@"Your String" 
           inImage:[UIImage imageNamed:@"empty_image.png"] 
           atPoint:CGPointMake(15, 25)]; 



-(UIImage*) drawText:(NSString*) text 
      inImage:(UIImage*) image 
      atPoint:(CGPoint) point 
{ 

    NSMutableAttributedString *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy]; 
    textStyle = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",text]]; 

    // text color 
    [textStyle addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, textStyle.length)]; 

    // text font 
    [textStyle addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20.0] range:NSMakeRange(0, textStyle.length)]; 

    UIGraphicsBeginImageContext(image.size); 
    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)]; 
    CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height); 
    [[UIColor whiteColor] set]; 

    // add text onto the image 
    [textStyle drawInRect:CGRectIntegral(rect)]; 

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return newImage; 
}