问题描述
我需要从UITextView或UILabel中获取UIImage。我有一个方法可以从其他类型的视图([UIViewController视图],MKMapView,UIButtons)成功提取图像,但我只是为我的UITextView获取白色矩形。
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.
非常感谢!
@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]];
}
和UIView代码中的grabImage
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;
}
推荐答案
技术是正确的。也许是因为文字被翻转了。您可以尝试为坐标系和原点设置变换。就像你在绘制文字时通常所做的那样。
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.
这篇关于从UITextView或UILabel中提取UIImage会产生白色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!