ios - 使用按钮索引以编程方式从 uiactionsheet 截取屏幕截图

标签 ios xcode uiactionsheet

我有一个 Action 方法来调用 UIActionsheet 来显示,从那里我想选择一个按钮来截取 View 的屏幕截图并使用 Mailcomposer 生成电子邮件。

调用 uiactionsheet 的 IBAction 方法工作正常。但按按钮拍摄快照不起作用。有帮助吗?

这是我的代码

 CGSize imageSize = [[UIScreen mainScreen] bounds].size;
        if (NULL != UIGraphicsBeginImageContextWithOptions)
            UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
        else
            UIGraphicsBeginImageContext(imageSize);

        CGContextRef context = UIGraphicsGetCurrentContext();

        // Iterate over every window from back to front
        for (UIWindow *window in [[UIApplication sharedApplication] windows])
        {
            if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
            {
                // -renderInContext: renders in the coordinate space of the layer,
                // so we must first apply the layer's geometry to the graphics context
                CGContextSaveGState(context);
                // Center the context around the window's anchor point
                CGContextTranslateCTM(context, [window center].x, [window center].y);
                // Apply the window's transform about the anchor point
                CGContextConcatCTM(context, [window transform]);
                // Offset by the portion of the bounds left of and above the anchor point
                CGContextTranslateCTM(context,
                                      -[window bounds].size.width * [[window layer] anchorPoint].x,
                                      -[window bounds].size.height * [[window layer] anchorPoint].y);

                // Render the layer hierarchy to the current context
                [[window layer] renderInContext:context];

                // Restore the context
                CGContextRestoreGState(context);
            }
        }

        // Retrieve the screenshot image
        UIImage *imageForEmail = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

      //  return imageForEmail;



     //       UIImageWriteToSavedPhotosAlbum(imageForEmail, nil, nil, nil);

    MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
    mailController.mailComposeDelegate = self;
    [mailController setSubject:@"Screenshot"];
    NSData *imageDataForEmail = UIImageJPEGRepresentation(imageForEmail, 1.0);
    [mailController addAttachmentData:imageDataForEmail mimeType:@"image/jpeg" fileName:@"ABC.jpg"];

    [self presentViewController:mailController animated:YES completion:NULL];

}

最佳答案

您没有将 UIActionSheet 的委托(delegate)设置为 self。将 UIActionSheetDelegate 添加到您的 .h 中,并将按钮操作代码替换为以下代码

-(IBAction) shareButtonClicked:(id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Take a Screenshot and share with"
                                                     delegate:self
                                            cancelButtonTitle:@"cancel"
                                       destructiveButtonTitle:nil
                                            otherButtonTitles:@"Email", @"Message", @"Facebook", @"Twitter", nil];
    [actionSheet showInView:self.view];
}

关于ios - 使用按钮索引以编程方式从 uiactionsheet 截取屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15156524/

相关文章:

swift - 如何将 Swift Package Manager 与现有的 macOS 项目一起使用?

ios - 使用GROUP APP在两个目标中共享文件

swift - 将 destructiveButtonTitle 移动到 UIActionSheet 的底部

iphone - 当用户点击 iPhone 中的上方 View 时隐藏 UIActionSheet

swift : OverlayView that appears at the bottom of the screen

ios - NSPredicate 格式动态使用属性名称

iOS swift : Google Maps and MapKit access doubles memory usage

ios - 在视网膜屏幕上对CALayer的0.5点(1像素)边框进行抗锯齿时闪烁

ios - iOS 8 中的 SevenSwitch UI 背景颜色

xcode - IOS 13.2 (17B84) Xcode 设备支持文件