7

I need to grab an UIImage from a UITextView or UILabel. I've got a method that will pull an image successfully from other types of views ( [UIViewController view], MKMapView, UIButtons) but I am just getting a white rect for my UITextView.

I've been banging my head against the wall for a while and suspect something really, really basic.

many thanks!

@interface TaskAccomplishmentViewController : UIViewController {

    MKMapView *mapView;
    UILabel *timeLeftText;
    UITextView *challengeText;

    UIButton *successButton;

<snip>

- (void) setChallangeImageToImageFromChallenge {

    // works
    // [currChallenge setChallengeImage:[UIImageUtils grabImageFromView:mapView]];
    // [currChallenge setChallengeImage:[UIImageUtils grabImageFromView:[self view]]];
    // [currChallenge setChallengeImage:[UIImageUtils grabImageFromView:successButton]];

    // doesn't work
    // [currChallenge setChallengeImage:[UIImageUtils grabImageFromView:timeLeftText]];
    [currChallenge setChallengeImage:[UIImageUtils grabImageFromView:challengeText]];

}

and the grabImage from a UIView code

+(UIImage *)grabImageFromView: (UIView *) viewToGrab {

    UIGraphicsBeginImageContext(viewToGrab.bounds.size);

    [[viewToGrab layer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}
Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
Oldmicah
  • 170
  • 1
  • 10
  • It's a UIImage, not an UIImage, as you pronounce the U as "you", not "ooh-eee-image". – Grant Paul Mar 15 '10 at 00:15
  • That's why I did "an UIImage" and "a UITextView". It was a prisoners' dilemma kind of answer, I'd rather be half right that totally wrong..... really, it was intentional... really. :) – Oldmicah Mar 15 '10 at 11:33
  • To get a sharp image also on retina displays, I needed to replace `UIGraphicsBeginImageContext(viewToGrab.bounds.size);` by this: `UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0.0);` as asked in [this question](http://stackoverflow.com/q/4334233/2471006) and explained in its accepted answer. Perhaps you might want to update your answer including that information. – anneblue Jul 22 '13 at 10:05

2 Answers2

3

The technique is correct. Maybe it is because text is flipped. You could try to set a transform for the coordinate system and origin. Like you would normally do when drawing text.

Stefan Arentz
  • 34,311
  • 8
  • 67
  • 88
  • Hello St3fan, Thank you. I took what you said to heart and went back to check the resultant image size and orientation, with the intention of manipulating the image as you suggested. Somewhere in the intervening code refactoring, nib restructuring, and reboot, it started working. My working theory at the moment is that I didn't have the challengeText view hooked up properly or that the image grab code was getting called before the view had drawn on screen. Either way, the code above does seem to work and thanks to everyone for taking a look at it! . – Oldmicah Mar 15 '10 at 14:08
0

I was having the same problem, getting a white rect instead of the text. To solve this, instead of using UIGraphicsBeginImageContext I used UIGraphicsBeginImageContextWithOptions and passed NO for opaque (second argument), that did the trick.

Also passed 0.0 for scale (third argument) and to get a context with a scale factor equal to that of the screen (to look good in retina device in my case).

Check the Apple doc about UIGraphicsBeginImageContextWithOptions here.

Miguel Gomes
  • 139
  • 2
  • 7