【问题标题】:Issue with attaching an Image file with mail使用邮件附加图像文件的问题
【发布时间】:2013-07-20 04:54:14
【问题描述】:

我正在尝试将视图捕获为图像,然后通过邮件附加该图像,但问题是捕获视图后图像周围出现白色边框! ,这个问题只发生在 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];


}

ViewController.m

然后捕获视图:

- (IBAction)mail:(id)sender {


 [shareIt mailAttachmentWithImage:_captureView openInView:self];

}

【问题讨论】:

  • 请提供有关您正在捕获的视图的更多信息。该视图预捕获的屏幕截图?
  • @AlfieHanssen 查看我编辑的问题,它只发生在 iPhone 5 上!!!真的很奇怪!!!
  • 你帖子中的图片(那个人的照片)是你截取的截图吗?还是您从中捕获的视图?从表面上看,我猜您正在捕获一个比图像本身更大的边界框,从而产生白色边框。如果它只是发生在 iPhone 5 上,是否意味着它没有发生在其他设备上?
  • @AlfieHanssen 不!它捕获的图像!这就是发生的事情!图像周围出现白色边框
  • 也许值得一试... [customScreenShot.layer setBorderColor: [[UIColor clearColor] CGColor]]; - 这会将图层的边框颜色设置为清除。

标签: iphone ios objective-c


【解决方案1】:

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

-(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];


}

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 对别人没有帮助!
    • 请在您的回答器中提供有关解决方案的更多信息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    相关资源
    最近更新 更多