【问题标题】:Save one page of UIScrollView to image将一页 UIScrollView 保存到图像
【发布时间】:2012-10-08 09:26:38
【问题描述】:

我有一个UIWebView。将数据加载到 UIWebView 后,我的 UIWebView 可以水平滚动,并且我在 web 视图中为我的 scrollView 启用分页。

我的问题是:

有什么方法可以将 UIWebView 中 scrollView 中的页面保存为图像?我的意思是,我会为 5 页滚动视图(内容大小 = 5 页)获得 5 张图片

【问题讨论】:

  • 如果您有滚动视图或 webview 等应用程序,但如果您想使用图像对象捕获和保存当前屏幕,请使用下面的代码...
  • 如果 web view 中的 UIScrollView 的内容尺寸大于 web view 中 uiscrollview 的 frame,则图片不完整

标签: iphone ios image uiwebview uiscrollview


【解决方案1】:

你可以像这样保存你的屏幕:-

 -(IBAction)savebutn:(id)sender
 {
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(saveImage);
NSFileManager *fileMan = [NSFileManager defaultManager];

NSString *fileName = [NSString stringWithFormat:@"%d.png",imagename];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
[fileMan createFileAtPath:pdfFileName contents:imageData attributes:nil];


 }

您的图片将保存在您的文档目录中,扩展名为 .png

【讨论】:

    【解决方案2】:

    此代码在用户想要捕获当前视图 ScreenShot 并共享或保存此图像时很有用....

    - (UIImage *)captureView {
    
    //hide controls if needed
    CGRect rect = [self.view bounds];
    
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [self.view.layer renderInContext:context];   
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return img;
    
    }
    

    查看我的博客,...http://parasjoshi3.blogspot.in/2012/06/captureimagescreenshot-of-view.html

    :)

    【讨论】:

    • 这个示例代码会将当前视图保存到图像,我已经尝试过了,但是,我的 UIWebView 的内容大小大于屏幕大小,所以我保存的图像有 UIWebView 的空内容
    • 不,我使用 [self.view bounds];我的视图有一个标签栏和一个 uiwebview。而且我的 uiwebview 的内容大小大于屏幕大小,当我使用上面的代码时,输​​出图像仅包含标签栏...
    • 试试这条线 CGRect rect = [yourScrollView bounds];插入这一行 CGRect rect = [self.view bounds];用我的代码...
    【解决方案3】:
    CGRect rect = [webview bounds];
    UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [webview.layer renderInContext:context];   
    UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    这有助于捕捉当前图像

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-19
      • 2017-12-26
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 1970-01-01
      相关资源
      最近更新 更多