How to render images from UIWebview
I am looking to load Webv开发者_运维技巧iew with URL and get content(images) of that.Then scale it to display properly in small size of WebView ot Imageview.
How to do that ?
Thanks,
Try something like this in your webView delegate:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGSize thumbsize = CGSizeMake(100,100);
UIGraphicsBeginImageContext(thumbsize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat scalingFactor = thumbsize.width/webView.frame.size.width;
CGContextScaleCTM(context, scalingFactor,scalingFactor);
[webView.layer renderInContext: UIGraphicsGetCurrentContext()];
UIImage *thumbImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//throw it on screen, just for testing
UIImageView *imgView = [[[UIImageView alloc] initWithImage:thumbImage] autorelease];
[self.view addSubview:imgView];
}
In most cases, you make an UIImage out of a UIView's CALayer. See How to take an iPhone screenshot of entire view including parts off screen? for details.
You can then resize the image:
The simplest way to resize an UIImage?
精彩评论