首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以编程方式从uiactionsheet获取带有按钮索引的屏幕截图

以编程方式从uiactionsheet获取带有按钮索引的屏幕截图
EN

Stack Overflow用户
提问于 2013-03-01 19:22:55
回答 1查看 291关注 0票数 0

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

调用uiactionsheet的IBAction方法工作正常。但是按下一个按钮来拍摄快照是不起作用的。有什么帮助吗?

以下是我的代码

代码语言:javascript
复制
 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];

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-01 22:16:20

不要将UIActionSheet的委托设置为self。将UIActionSheetDelegate添加到您的.h中,并用以下代码替换按钮操作代码

代码语言:javascript
复制
-(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];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15156524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档