截图 UIImageView 和 UILabel 然后分享

Take Screenshot of UIImageView and UILabel then Share it

我想我快要解决问题了。我有一个 UItableView 的原型单元格,如下所示:
您看到的 灰度背景图像 来自服务器,文本是 叠加文本 这是 UILabel 并且会在服务器中的每个图像上发生变化。所以这就像我要发送的 两层 。第三个红色按钮是分享按钮,说我select在电子邮件中分享这张图片这个函数将被调用:

-(UIImage *)makeImageofCell:(NSUInteger)sender
{
    HomeCell *cellObj;
    cellObj.tag = sender;
    UIGraphicsBeginImageContext(cellObj.homeImg.frame.size );
    [cellObj.homeImg.layer renderInContext:UIGraphicsGetCurrentContext()];
    [cellObj.overlayText.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}

-(void)shareButtonClicked:(UIButton*)sender我写了分享的代码。现在在这个函数中我想发送那个图像。这是我正在做的事情:

UIImage *imageToShare = [self makeImageofCell:sender.tag]; 调用 makeImageofCell 函数并将它的值赋给 UIImage。所以我写这篇文章是为了分享 URL.

NSString *imageToShare = [NSString stringWithFormat:@"Hey! Check out this Image %@",imageToShare];


但我得到的只是 Image is: (null)。知道如何实现吗?

UIWindow 的屏幕截图请遵循以下代码您只需替换为自定义视图或单元格即可。

- (UIImage*)takeScreenshot
{
    // get the key-window references
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];

    // manipulate boundries of key-window
    CGRect rect = [keyWindow bounds];

    // create context using size
    UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);

    // get the context reference
    CGContextRef context = UIGraphicsGetCurrentContext();

    // render in context
    [keyWindow.layer renderInContext:context];

    // get the rendered image
    UIImage *capturedScreen = UIGraphicsGetImageFromCurrentImageContext();

    // complete context
    UIGraphicsEndImageContext();

    // return image
    return takeScreenshot;
}

已编辑

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
         UIButton *btnShareImage = (UIButton*)[cell1 viewWithTag:KTAGABSENTBUTTON];
         [btnShareImage addTarget:self action:@selector(shareImageAction:) forControlEvents:UIControlEventTouchUpInside];
         [btnShareImage setTitle:[NSString stringWithFormat:@"%li %li",(long)indexPath.section,(long)indexPath.row] forState:UIControlStateDisabled];
         return cell;
     }
    -(void)shareImageAction:(UIButton*)sender{
        NSString *str=[sender titleForState:UIControlStateDisabled];
        NSArray *ar=[str componentsSeparatedByString:@" "];
        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:[[ar objectAtIndex:1] intValue] inSection:[[ar objectAtIndex:0] intValue]];

        HomeCell *cellObj = (HomeCell*) [yourTableView cellForRowAtIndexPath:indexPath];
        UIImage *imageToShare = [self makeImageofCell:cellObj];

    }
    -(UIImage *)makeImageofCell:(HomeCell*)cellObj
    {
        UIGraphicsBeginImageContext(cellObj.homeImg.frame.size );
        [cellObj.homeImg.layer renderInContext:UIGraphicsGetCurrentContext()];
        [cellObj.overlayText.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return viewImage;
    }

希望对您有所帮助。