0

UIActionsheetを呼び出して表示するActionメソッドがあり、そこからボタンを選択してビューのスクリーンショットを撮り、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];

}
4

1 に答える 1

1

UIActionSheet のデリゲートを自分自身に設定しません。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];
}
于 2013-03-01T14:16:19.867 に答える