1

I create a UIImageView with two UILabel as subviews of it:

 UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, [[UIScreen mainScreen] bounds].size.height)];
    img.backgroundColor=self.view.backgroundColor;

    UILabel *textOne=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 290, 250)];
    textOne.text=_textOneLabel.text;
    textOne.textColor=_textOneLabel.textColor;

    UILabel *textTwo = [[UILabel alloc] initWithFrame:CGRectMake(100,180,200,100)];
    textTwo.text=_textTwoLabel.text;
    textTwo.textColor=_textTwoLabel.textColor;

    [img addSubview:textOne];
    [img addSubview:textTwo];

    UIImageWriteToSavedPhotosAlbum(img.image, nil, nil, nil); //img.image is null

I want to create a UIImage to save it in the camera roll with the UIImageView and the 2 created UILabel. img.image returns null because there is no image assigned to the UIImageView.
Do you have an idea of how achieving that?

2
  • Are you trying to burn text on image? Commented Dec 21, 2013 at 13:34
  • You haven't added any image to image view's image property, you only have specified its background color property. Commented Dec 21, 2013 at 13:35

2 Answers 2

6

Use below method to convert your UIImageView to UIImage.

UIImage *image = [self ChangeImageViewToImage:img];

//Method

-(UIImage *) ChangeImageViewToImage : (UIImageView *) view{

 UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
 [view.layer renderInContext:UIGraphicsGetCurrentContext()];

 UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

 UIGraphicsEndImageContext();

 return img;

}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much for your great answer, however, I have a small problem, is that the image is a little bit blurred. Can I optimize it?
Use the code I updated just now. It will give you retina image
Try this: UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
I tried in my iPhone, and not in the simulator, and the image is not blurred. Well, you made my day Mavericks. Thank you very much. Accepted ;).
1

This should help you

/* Creates an image with a home-grown graphics context, burns the supplied string into it. */
- (UIImage *)burnTextIntoImage:(NSString *)text :(UIImage *)img {

UIGraphicsBeginImageContext(img.size);

CGRect aRectangle = CGRectMake(0,0, img.size.width, img.size.height);
[img drawInRect:aRectangle];

[[UIColor redColor] set];           // set text color
NSInteger fontSize = 14;
if ( [text length] > 200 ) {
    fontSize = 10;
}
UIFont *font = [UIFont boldSystemFontOfSize: fontSize];     // set text font

[ text drawInRect : aRectangle                      // render the text
         withFont : font
    lineBreakMode : UILineBreakModeTailTruncation  // clip overflow from end of last line
        alignment : UITextAlignmentCenter ];

UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext();   // extract the image
UIGraphicsEndImageContext();     // clean  up the context.
return theImage;
}

2 Comments

I already have the texts(UILabels) with the font, I just want to save the UIImageView + the 2 created UILabels into one UIImage.
That is what this function will do. You need to tweak the function a bit to take in as many UILabels and UIImageViewyou want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.