从 UITextView 或 UILabel 中提取 UIImage 会给出白色图像
我需要从 UITextView 或 UILabel 中获取 UIImage。我有一个方法可以从其他类型的视图([UIViewController view]、MKMapView、UIButtons)成功提取图像,但我只是为 UITextView 获取一个白色矩形。
我已经用头撞墙有一段时间了,并怀疑一些非常非常基本的事情。
非常感谢!
@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
+(UIImage *)grabImageFromView: (UIView *) viewToGrab {
UIGraphicsBeginImageContext(viewToGrab.bounds.size);
[[viewToGrab layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该技术是正确的。可能是因为文字被翻转了。您可以尝试设置坐标系和原点的变换。就像您在绘制文本时通常所做的那样。
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.
我遇到了同样的问题,得到一个白色矩形而不是文本。为了解决这个问题,我没有使用 UIGraphicsBeginImageContext,而是使用了
UIGraphicsBeginImageContextWithOptions
并通过 NO 表示不透明(第二个参数),这样就成功了。还传递了 0.0 作为比例(第三个参数),并获得比例因子等于屏幕比例因子的上下文(在我的例子中,在视网膜设备中看起来不错)。
检查有关
UIGraphicsBeginImageContextWithOptions
的 Apple 文档 此处。I was having the same problem, getting a white rect instead of the text. To solve this, instead of using
UIGraphicsBeginImageContext
I usedUIGraphicsBeginImageContextWithOptions
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.