从不同的.XIB中捕获UIImageView

时间:2011-05-17 00:03:43

标签: iphone objective-c xcode xcode4

来自这个神圣圣地的人,

新问题!我需要从不同的.XIB获取UIImageView。 让我把它放在代码中,这样就更简单了:

1)

-(UIImage *)grabScreenImage{

    //screen = IBOutlet for the UIImageView

    UIGraphicsBeginImageContext(self.screen.frame.size);
    [(CALayer *)self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSLog(@"PASSED BY THE PHOTO DUDES");
    return image;

}

2)

-(UIImage *)takeGraphScreenImage{

//shouldBeGraphViewController = IBOutlet for the other UIViewController
//shouldBeGraph = IBOutLet for the UIImageView in the ShouldBeGraphViewController

        UIGraphicsBeginImageContext(shouldBeGraphViewController.shouldBeTheGraph.frame.size);
        [(CALayer *)shouldBeGraphViewController.shouldBeTheGraph.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsGetCurrentContext();
        return image;
    }

(1)工作正常,我捕获图像并通过电子邮件发送,但(2)没有。

我刚刚复制并粘贴并更改了一些参数,因此它可以被其他参数使用,但它会给我带来大量错误。

3) - (IBAction)sendMail {

MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc]init];

UIImage *curretScreen = [[self takeGrabScreenImage]retain];
UIImage *graphScreen = [[self takeGraphScreenImage]retain];

if ([MFMailComposeViewController canSendMail]){
    NSLog(@"ENTERED THE GOOD PART OF THE IF");
    mailComposer.mailComposeDelegate = self;
    [mailComposer setSubject:@"MY SCREENSHOT TEST"];
    [mailComposer addAttachmentData:UIImagePNGRepresentation(curretScreen) mimeType:@"image/png" fileName:@"SCREENSHOT"];
    [mailComposer addAttachmentData:UIImagePNGRepresentation(graphScreen) mimeType:@"image/png" fileName:@"SCREENSHOT-02"];

    //NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //NSString* documentsDirectory = [paths objectAtIndex:0];

    /*for (NSString *x in [self.photoLocations allKeys]){
        NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:x];
        UIImage *item = [UIImage imageWithContentsOfFile:fullPath];
        [mailComposer addAttachmentData:UIImagePNGRepresentation(item, 0.5) mimeType:@"image/jpeg" fileName:
    }*/

} else { ... }

[mailComposer setMessageBody:@"PUT YOUR MESSAGE HERE" isHTML:NO];
[self presentModalViewController:mailComposer animated:YES];
[mailComposer release];
[curretScreen release];

}

我只需要从两个UIImageViews(来自两个UIViewControllers)截取屏幕截图并通过电子邮件发送,电子邮件部分已完成并拍摄第一个屏幕截图,但第二个屏幕截图没有。给我这样的错误:

Mon May 16 21:02:46 FGringo EmailSending[1036] <Error>: CGContextSaveGState: invalid context 0x0
Mon May 16 21:02:46 FGringo EmailSending[1036] <Error>: CGContextSetAlpha: invalid context 0x0

请帮帮我! (:

1 个答案:

答案 0 :(得分:0)

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Dismiss image picker modal.
    [picker dismissModalViewControllerAnimated:YES];

    if ([MFMailComposeViewController canSendMail]) {
        // Create a string with HTML formatting for the email body.
        NSMutableString *emailBody = [[NSMutableString alloc] initWithString:@"<html><body>"];

        // Add some text to it.
        [emailBody appendString:@"<p>Body text goes here.</p>"];

        // You could repeat here with more text or images, otherwise
        // close the HTML formatting.
        [emailBody appendString:@"</body></html>"];
        NSLog(@"%@", emailBody);

        // Create the mail composer window.
        MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
        emailDialog.mailComposeDelegate = self;

        // Image to insert.
        UIImage *emailImage = [info objectForKey:UIImagePickerControllerOriginalImage];

        if (emailImage != nil) {
            NSData *data = UIImagePNGRepresentation(emailImage);
            [emailDialog addAttachmentData:data mimeType:@"image/png" fileName:@"filename_goes_here.png"];
        }

        [emailDialog setSubject:@"Subject goes here."];
        [emailDialog setMessageBody:emailBody isHTML:YES];

        [self presentModalViewController:emailDialog animated:YES];
        [emailDialog release];
        [emailBody release];
    }
}