-9

我正在尝试将视图捕获为图像,然后通过邮件附加该图像,但问题是在捕获视图后,图像周围会出现白色边框!,这个问题只发生在iPhone 5设备上!这是我的代码:

分享.m

- (void)mailAttachmentWithImage:(UIView*)view openInView:(UIViewController*)viewCont {

    MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
    controller.mailComposeDelegate = self;

    UIView* captureView = view;
    captureView.backgroundColor = [UIColor clearColor];
    /* Capture the screen shoot at native resolution */
    UIGraphicsBeginImageContextWithOptions(captureView.bounds.size, NO, 0.0);
    [captureView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    /* Render the screen shot at custom resolution */
    CGRect cropRect = CGRectMake(0 ,0 ,1024 ,1024);
    UIGraphicsBeginImageContextWithOptions(cropRect.size, NO, 1.0f);
    [screenshot drawInRect:cropRect];
    UIImage * customScreenShot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSData *myData = UIImagePNGRepresentation(customScreenShot);
    [controller addAttachmentData:myData mimeType:@"image/png" fileName:@"Image"];


    [viewCont presentViewController:controller animated:YES completion:nil];


}

视图控制器.m

然后捕获视图:

- (IBAction)mail:(id)sender {


 [shareIt mailAttachmentWithImage:_captureView openInView:self];

}

在此处输入图像描述

4

2 回答 2

2

您需要捕获图像Image-view而不是UIView. 您发布的答案不足以帮助其他人。作为我的波纹管代码,您可以Image-view尝试使用波纹管代码捕获框架的图像:-

-(void)imageWithView:(UIView *)view
{
      CGRect rect = CGRectMake(imgview.frame.origin.x, imgview.frame.origin.y,imgview.frame.size.width, imgview.frame.size.height);

    UIGraphicsBeginImageContextWithOptions(self.view.frame.size, YES, 1.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();


   // CGRect rect = CGRectMake(0,0,10,10);
    CGImageRef imageRef = CGImageCreateWithImageInRect([screenshot CGImage], rect);
    UIImage *croppedScreenshot = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];

    NSData *imageData = UIImagePNGRepresentation(croppedScreenshot);

   [controller addAttachmentData:imageData mimeType:@"image/png" fileName:@"Image"];
   [viewCont presentViewController:controller animated:YES completion:nil];


}

因此,此捕获的图像不包含边框,您可以将此无边框图像附加到电子邮件中。

于 2013-07-30T05:21:10.387 回答
0

问题解决了 !因为我的视图大于目标图像大小!!!!

于 2013-07-23T13:54:33.330 回答