Place Image from UIWebView into UIImageView
I have a UIWebView hidden in the background of my app that loads a site, and I want to place one of the images loaded from it into a visible UIIma开发者_JAVA百科geView. Because the image I'm loading is a PHP session-based captcha, I can't just load the image directly from it's URL into the UIImageView, I have to get the particular instance that is loaded into the UIWebView. Can anyone give me a couple of pointers on how I would go about approaching this? I can't seem to know where to start...
This snippet grabs a 50x50 pixel square with an origin of (50,50) on the webview. Adjust the positions to the position of the captcha (if you know it is always in the same place).
UIImage* theImage;
UIGraphicsBeginImageContext(CGSizeMake(50,50));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, -50, -50 );
[[[self webView] layer] renderInContext:context];
theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// now do stuff with theImage
You may find you need to put this code (or your version of it) in a webViewDidFinishLoad: method in the webView delegate. Webviews, even basic ones, often take ages to render. If you grab the image too soon, you won't get what you want.
Hi If you can modify your PHP page, create a Javascript which give you the url of the captcha image. after use the stringByEvaluatingJavaScriptFromString on your UIWebView
What you can try :
- Make a javascript which get the image data as string (try canvas.getImageData, not sure it's implemented in Safari)
- Get this string with stringByEvaluatingJavaScriptFromString
- Convert the NSString to NSData
- And after init an image with NSData...
精彩评论