uiwebview拍摄屏幕截图ios

时间:2013-11-28 11:27:29

标签: ios uiwebview uikit screenshot

我加载了一个带有在线Microsoft Word文档的Web视图,比如46页。为了方便用户,我需要将页面显示为缩略图。 我所做的是通过改变每页的偏移量来为uiWebView的每个缩略图拍摄屏幕截图。 这是代码

-(void)takeScreenShotFromWebViewWithStartOffset:(int)currentOffset andEndOffset:(int)endOffset
{
// noOfScreenShotsInOneGo is 10 , i take load thumbnails lazily. first load 10 and when user scrols to the last row of the horizontal table view i load next 10 and so on.

       for (int screenshot= 0; screenshot<noOfScreenShotsInOneGo; screenshot++) {
        [[webView scrollView] setContentOffset:CGPointMake(0, currentOffset)];
        if(currentOffset<endOffset)
        {
            currentOffset = currentOffset+480; // 480 is the screen height


        UIImage *img = [self takeScreenShotFromView:webView withWidth:320 andHeight:480];
        [self.thumbnailsArray addObject:img];
        }
    }


}


+ (UIImage *) takeScreenShotFromView: (UIWebView *) webview withWidth: (float)width andHeight: (float)height
{
    UIGraphicsBeginImageContext(CGSizeMake(width, height));
    [webview.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    img = [self imageWithImage:img scaledToWidth:60];
    return img;
}

+(UIImage*)imageWithImage: (UIImage*) sourceImage scaledToWidth: (float) i_width
{
    float oldWidth = sourceImage.size.width;
    float scaleFactor = i_width / oldWidth;

    float newHeight = sourceImage.size.height * scaleFactor;
    float newWidth = oldWidth * scaleFactor;

    UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight));
    [sourceImage drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSLog(@"imageWidth:%f height:%f",newImage.size.width,newImage.size.height);
    return newImage;
}

我从循环中调用此方法并获取10个缩略图(每次我将页面高度添加到内容偏移量),并将其显示在屏幕底部的水平表视图中。

此代码在iod 6和7的ipod 5上运行正常,但在运行代码时ipod touch 4应用程序崩溃(带内存警告)。但是,如果我拍摄几张具有相同内容偏移量的屏幕截图,它可以正常工作。结果是,仅显示所有缩略图的第一页图像。我认为在拍摄屏幕截图中存在一些问题,然后更改webview的偏移量,然后采用屏幕截图(等等)方法。 请帮助我创建缩略图的技巧是错误的?或者内存占用量增加太多,操作系统决定杀死应用程序?

0 个答案:

没有答案