将uilabel和uiimageview保存为uiimage

Ash*_*ein 2 uiimage uilabel imageview ios

我正在尝试向图像添加文本,但uilabel在a中添加as子视图uiimageview.我已经这样做但我想将它们保存为图像; 我在上下文中使用渲染,但它不起作用.

这是我的代码:

UIImage * img = [UIImage imageNamed:@"IMG_1650.JPG"];

        float x = (img.size.width/imageView.frame.size.width) * touchPoint.x;
        float y = (img.size.height/imageView.frame.size.height) * touchPoint.y;

        CGPoint tpoint = CGPointMake(x, y);

        UIFont *font = [UIFont boldSystemFontOfSize:30];
        context = UIGraphicsGetCurrentContext();
        UIGraphicsBeginImageContextWithOptions(img.size, YES, 0.0);
        [[UIColor redColor] set];

        for (UIView * view in [imageView subviews]){
            [view removeFromSuperview];
        }
             UILabel * lbl = [[UILabel alloc]init];
        [lbl setText:txt];
        [lbl setBackgroundColor:[UIColor clearColor]];
        CGSize sz = [txt sizeWithFont:lbl.font];
        [lbl setFrame:CGRectMake(touchPoint.x, touchPoint.y, sz.width, sz.height)];
        lbl.transform = CGAffineTransformMakeRotation( -M_PI/4 );

        [imageView addSubview:lbl];
        [imageView bringSubviewToFront:lbl];
        [imageView setImage:img];
        [imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
        [lbl.layer renderInContext:UIGraphicsGetCurrentContext()];

        UIImage * nImg = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        UIImageWriteToSavedPhotosAlbum(nImg, nil, nil, nil);
Run Code Online (Sandbox Code Playgroud)

Nek*_*kto 5

请尝试下面的代码.这个对我有用.

UIGraphicsBeginImageContext(imageView.bounds.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *bitmap = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

PS我认为你不需要renderInContext UILabel分层.