从UITextView或UILabel中提取UIImage会产生白色图像

Old*_*cah 7 iphone uitextview uiview uiimage uilabel

我需要从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]];

}
Run Code Online (Sandbox Code Playgroud)

和来自UIView代码的grabImage

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

    UIGraphicsBeginImageContext(viewToGrab.bounds.size);

    [[viewToGrab layer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*ntz 3

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