【问题标题】:Take Screenshot of UIImageView and UILabel then Share it截取 UIImageView 和 UILabel 的截图然后分享
【发布时间】:2015-08-03 09:04:26
【问题描述】:

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

-(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)。知道如何实现吗?

【问题讨论】:

    标签: ios objective-c uitableview uiimageview uiimage


    【解决方案1】:

    请按照 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;
        }
    

    希望对您有所帮助。

    【讨论】:

    • UITableView 参与其中,所以我认为tags 有问题。截图代码工作得很好,我在这个项目中实现之前已经测试过了。
    • KTAGABSENTBUTTON 这是什么?
    • 如果你有那个单元格引用,它只是作为单元格按钮的参考,那么你可以像明智地使用 UIButton *btnShareImage =cell.yourButtonVariableName。
    • 所以我在KTAGABSENTBUTTON 的地方使用了viewWithTag:cellObj.shareBtn.tag,但应用程序总是在第二行[btnShareImage addTarget:self action:@selector(shareImageAction:) forControlEvents:UIControlEventTouchUpInside];cellForRowAtIndexPath 方法中崩溃,这是已知错误libc++abi.dylib: terminating with uncaught exception of type NSException
    猜你喜欢
    • 1970-01-01
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多