【发布时间】:2011-01-27 11:58:42
【问题描述】:
我需要从 UITextView 或 UILabel 中获取 UIImage。我有一种方法可以成功地从其他类型的视图([UIViewController 视图]、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;
}
【问题讨论】:
-
这是一个 UIImage,而不是 UIImage,因为你将 U 发音为“you”,而不是“ooh-eee-image”。 纳粹> -
这就是为什么我做了“一个 UIImage”和“一个 UITextView”。这是一个囚徒困境的答案,我宁愿对一半完全错误......真的,这是故意的......真的。 :)
-
为了在视网膜显示器上也能获得清晰的图像,我需要将
UIGraphicsBeginImageContext(viewToGrab.bounds.size);替换为:UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0.0);,正如this question 中所要求的那样,并在其接受的答案中进行了解释。也许您可能想更新您的答案,包括该信息。
标签: iphone uiview uiimage uitextview uilabel