【问题标题】:Catch a UIImageView from a different .XIB从不同的 .XIB 中捕获 UIImageView
【发布时间】:2011-05-17 00:03:42
【问题描述】:

来自这个神圣避难所的家伙,

新问题!我需要从不同的.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

请帮帮我! (:

【问题讨论】:

    标签: iphone objective-c xcode xcode4


    【解决方案1】:
    - (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];
        }
    }
    

    【讨论】:

    • 并非如此。因为我已经有发送和附加的代码,但正如我解释的那样,问题是:我在一个视图“主要视图”中,我需要从这个“主要视图”和“辅助视图”中获取“截图”,但是“发送邮件”按钮位于“主体视图”上。
    猜你喜欢
    • 2012-03-24
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    • 2015-01-09
    • 1970-01-01
    相关资源
    最近更新 更多