create a UIImage from points - iphone

I have a UIWebView which has images in it, say that I have the point/coordinates of the x y position of the image, width and height. Can I create an image just by specifying the location of the image in UIWebView? If yes how?
Or is there a way to get an UIImage from a UIWebView without actually looking at the <img src=""> of the html tags? Basically I don't want to make a connection to the internet to pull an image as the image is already there seen on the screen. Question is how do I extract it?

Depending on the cache policy in place, a request to retrieve the image may not result in another connection to download the image... But to answer your actual question...
You can take a 'snapshot' of the UIWebView and then crop to the location of the image in question. Check this question out for details on how to do this: How Do I Take a Screen Shot of a UIView?

This link explain a way to get the image just by grabbing it from the specified coordinates. How to create a UIImage from the current graphics context?.
However there might be a more elegant solution of using some javascript to get the original image by using the stringByEvaluatingJavaScriptFromString: method of UIWebview. But I am not sure if javascript is capable of doing it

Related

UIImageView Image from NSURLConnection

I have a function that retrieves some images from a JSON feed using NSURLConnection. My connectionDidFinishLoading retrieves an array with all the data for that image (including the URL of the image).
What's the best way to display that image on a UIImageView? Even though the URL is being retrieved, I am pretty sure I would still have to display the image in a connection-friendly manner?
Thank you,
As per your requirement Images must be display in connection friendly manner. Then you can go for Image cacheing which requires image URLs. For your best understanding see this link and this link.
for asynchronous image downloading try with this link.

create fliping book with images

I want to create a application in which i required following function
create array of images in view and move like page turning effect orientation change then same view will execute. tap on image then zoom in or zoom out image.
double tap then upload bigger image from web-services.
on image when select part of image then open url of that image.
Try these Sample code
https://github.com/devindoty/iBooks-Flip-Animation
http://oleb.net/blog/2010/06/app-store-safe-page-curl-animations/
https://github.com/mtabini/AFKPageFlipper
AFKPageFlipper will help you with the page-flip-effect. The other questions: First, try to solve it self (zoom, ...) and if you have problem ask a new questions here.

How does One place an image inside of an image using metadata?

For example, I can convert the second image into NSData and then place it inside metadata inside the first image and then when I open the first image and read the metadata I can get the NSData and turn it into an UIImage.
How would I go about doing this? All the metadata tags I see are not large enough to support another picture. I know picture in picture is quite common on desktop apps so I'm interested in getting it to work on the iPhone.
Is metadata the correct way to do this or is there another way?

question regarding <img> and thumbnails

I have a photo site which renders previews of my photos in a tag when i hover some dots. (see http://johanberntsson.se/photo/).
But it feels kinda stupid to load the full image in to an img-tag. Because as far as i know, theres no difference between loading the image with its original size versus adding height and width attributes to it.
Got any suggestions how to improove my preview function?
Thanks
As far as client side is concerned, there's nothing you can do. If you're worried about how long it takes the image to load, you could always pre-load them with javascript. The only other thing would be to create a few sizes of your image on the server.
You should resize the image to get better speed. Here's an example in php
//Blob if image in database
$blob = mysql_fetch_...();
//else blob point to filename
$blob = "filename.jpg";
$gd = imagecreatefromstring($blob);
$resized = imagecreatetruecolor(X, Y); // new image dimensions
imagecopyresampled(....);
imagejpeg($resized);
Loading all the thumbnails in a giant sprite image used as a background image. Then position the background a the right place to have the right image a the right place.
This will improve the loading speed by loading only 1 image and allow the browser to cache the request of the image so, next time the user come to your site, it will load pretty fast.

Rendering a UIWebView into an ImageContext

I am trying to capture the contents of a UIWebView including that which is not visible to the user. i.e. The whole web page even though the user is only looking at the top.
Looking around I found the best way to capture a UIView is to retrieve its layer and use renderInContext.
However, UIWebView seems to be using its own CALayer implementation which is behaving a lot more like CATiledLayer, although it is still claiming to be a standard CALayer. When I call renderInContext I only get one portion of the web page, up to 940px down, as opposed to the whole page.
Has anyone got any ideas on how to either: force the UIWebView to scroll down another 940px (obviously that is far from ideal) or tell whatever breed of CALayer it is backing the WebView to render all of its content when I ask it to.
Cheers
EDIT: I should add that currently change the frame of the webview to fit the size of the page retrieved through javascript.
I've released an app (Web2Pic) doing that, and please trust me that UIGraphicsBeginImageContext(webView.frame.size);
can do nothing except getting a small image from the visible area in our UIWebView ;-(
The right way is a bit complex but it just works:
1.Use JavaScript in our UIWebView to get these float values:
//Whole page size in HTML coordinate
document.body.scrollWidth
document.body.scrollHeight
//UIWebView visible size in HTML coordinate
window.innerWidth
window.innerHeight
2.Now we can 'cut' the whole page into dozens of UIWebView-sized small pieces. Then we can capture every small pieces individually and save them into our Cache. I implemented this by calculating page-offsets and use UIGraphicsBeginImageContext(webView.frame.size); to get a array of images. In addition, you should cache the image array into the file system, or the app will eventually crash!
3.When we finally got all the small pieces, we can start a full-resolution context: UIGraphicsBeginImageContext(CGSizeMake(document.body.scrollWidth,document.body.scrollHeight));
4.Render every small images into the big context based on the coordinates. And be careful to the corners, the last image in every line/row may not be a full image.
5.There is still one step left: saving the big image. Do not save it into the PhotoAlbum, because iOS will automatically cut down the resolution of images in the album. Instead, we can save it into the file system and enable the app's iTunes File Share support, or even write a simple in-app photo manager.
Hope these can help ;-)
Yichao Peak Ji
It looks to me like UIWebView renders on demand (witness the checkerboard as you scroll downwards rapidly on a large page), so there won't be anything in the part of the layer below what you can reach until the user scrolls down there. The layer won't be able to write out what it doesn't have, so I don't think you'll be able to capture the whole area.
As far as scrolling, there aren't any obvious exposed API methods that I can think of to move it down. The web view seems to host a UIScrollView or something similar, which you could try to access by traversing the view hierarchy and use its scrollRectToVisible:animated:, but that doesn't sound like a good long-term solution.
Just to answer one of my questions, you can scroll the view by using javascript.
So you use stringByEvaluatingJavaScriptFromString: with a javascript of window.scrollTo(0,%i);, where %i is where you want to scroll to. You can also use scrollY to retrieve the current scroll value.
Resizing the Webview is the way I solved it. If you want a bigger image with more detail, you just have to zoom in before calling this method. Beware that large dimensions could infulence the performance.
- (UIImage*) imageFromWebview:(UIWebView*) webview{
//store the original framesize to put it back after the snapshot
CGRect originalFrame = webview.frame;
//get the width and height of webpage using js (you might need to use another call, this doesn't work always)
int webViewHeight = [[webview stringByEvaluatingJavaScriptFromString:#"document.body.scrollHeight;"] integerValue];
int webViewWidth = [[webview stringByEvaluatingJavaScriptFromString:#"document.body.scrollWidth;"] integerValue];
//set the webview's frames to match the size of the page
[webview setFrame:CGRectMake(0, 0, webViewWidth, webViewHeight)];
//make the snapshot
UIGraphicsBeginImageContextWithOptions(webview.frame.size, false, 0.0);
[webview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//set the webview's frame to the original size
[webview setFrame:originalFrame];
//and VOILA :)
return image;
}
All UIViews have a size limit of 1024x1024, so you will probably have programmatically scroll, capture the page in chunks, and stitch them together somehow.
Set the height of webview equal to the contentsize of scrollview of webview.
-(UIImage *)captureImage:(UIWebView *)webvw
{
webvw.frame=CGRectMake(webvw.frame.origin.x, webvw.frame.origin.y, webvw.frame.size.width, webvw.scrollView.contentSize.height);
UIGraphicsBeginImageContextWithOptions(webvw.frame.size, NO, 0.0f);
[webvw.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
return screenshot;
}