从 UITextView 或 UILabel 中提取 UIImage 会给出白色图像

发布于 2024-08-25 15:42:06 字数 1301 浏览 4 评论 0原文

我需要从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煮酒 2024-09-01 15:42:06

该技术是正确的。可能是因为文字被翻转了。您可以尝试设置坐标系和原点的变换。就像您在绘制文本时通常所做的那样。

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.

无边思念无边月 2024-09-01 15:42:06

我遇到了同样的问题,得到一个白色矩形而不是文本。为了解决这个问题,我没有使用 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文