我正在尝试将视图捕获为图像,然后通过邮件附加该图像,但问题是捕获视图后图像周围出现白色边框! ,这个问题只发生在 iPhone 5 设备上!这是我的代码:
Sharing.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];
}
答案 0 :(得分:2)
您需要捕获Image-view
而不是UIView
的图像。你发布的答案不足以帮助其他人。作为我的轰鸣声代码,您可以使用Bellow代码捕获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];
}
因此,此捕获的图像不包含边框,您可以将此图像无边框附加到电子邮件中。
答案 1 :(得分:0)
问题解决了!因为我的视图大于目标图像大小!!!!