Loading image from url problem iphone - iphone

I'm loading images from url into webviews but that's showing me images which are cut down. I tried doing sizeToFit, but that shows a very small image cornered at left in my webview as the webage is large and image at its upper left corner.
EDIT:
This' how I'm loading images in webview. Here, expanded_photo is webview.
NSString *urlAddress = [NSString stringWithFormat:#"%#",photo_url];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[expanded_photo loadRequest:requestObj];
Whenever I try loading it through uiimageview using a uiimage like following, Here expanded_photo is imageview which I'm creating in a nib file:
UIImage *image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:photo_url]]];
expanded_photo.image = image1;
This takes a lot of time to load.
I want a solution which can load image from url in a small amount of time and the image is not cut.
Can anybody please help?
Thanx in advance.

Be aware that -[NSData dataWithContentsOfURL:] loads the data synchronously and thus blocks your main thread until the download is finished. When you load multiple images that way, all those loading operations are executed sequentially. This is not only slow but also your app is unresponsive during loading.
You might consider moving the actual load request in a background thread (using NSOperationQueue) or use NSURLConnection to asynchronously load the image.

Related

Append url to image path NSString?

I'm developing an iOS application consisting of a static sqlite database, series of tableviews and tab based detail view where the first view loaded in the detailview is a swipable imageView which loads a series of images.
I've got it working with this code where it looks for the image locally but I'd like to have it load the image from a URL or display a default image if no internet connection is available.
The images are named in the database as (for the example) image.jpg and I'd like all of them to load from the same URL directory (for the example) http://www.someurl.com/images/
Thank you
- (UIImage *) imageAtIndex:(NSUInteger)index {
Image *currentImage = (Image *) [self.images objectAtIndex:index];
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%#",[[currentImage filename] stringByDeletingPathExtension]] ofType:#"jpg"];
return [UIImage imageWithContentsOfFile:path];
}
The process for this is really very simple:
Try to download your image data using NSURLConnection
If successful, create a UIImage from the data
If either of the above fails, switch over to your placeholder/built-in image
Display the resulting image
I advise you have some sort of placeholder while the image is downloading, since that can take quite a while.
Don't bother with reachability; it's not 100% reliable, whereas actually trying the download is.
Avoid doing this on a background thread or queue. NSURLConnection is asynchronous out of the box to make this easier for you. There are a ton of third-party frameworks that try to simplify working with connections if you wish, though.
NSString *urlStr = [#"http://www.someurl.com/images/" stringByAppendingString:[currentImage filename]];
NSURL *url = [NSURL URLWithString:urlStr];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];
Or something like that...
If the image is large or the internet connection is slow, the NSData initialization might take some time. Consider getting the images data in a background thread, or use some existing framework for that like SDWebImage

How to Load a WebView pre-zoomed in? (In Xcode / Objective C)

I am loading a UIWebview of a hosted .jpg. It's actually a schedule, so it is a rather large image. Instead of having users have to zoom in right away, I would like to load the web view already zoomed in. Although I still need the user to be able to zoom in and out, and scroll. Basically I am just looking for an "initial" zoom. How would I accomplish this? Just FYI I put the method below I am using to load the image...Thanks!
// Webview code
NSString *urlAddress = #"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webview loadRequest:requestObj];
In iOS 5 you can access the UIWebView's scrollView property and set the zoom level on it after the page has loaded. That would likely work as you wanted. To get the same thing on pre-iOS 5 you'd probably want to go through the UIWebView's subviews until you find a UIScrollView and do the same thing on that.
I don't think there's any other way to programatically zoom it. Unless you can do it with Javascript and execute some using UIWebView's stringByEvaluatingJavaScriptFromString: method.

How to fetch images for every UiTableViewCell efficently?

I receive and parse JSON from the internet in my app delegate. In JSON there are links to images that must be displayed in table cells (1 image per cell).
If i fetch images in cellForRowAtIndexPath method, it takes quite some time to load the whole view. I use this code in cellForRowAtIndexPath:
NSURL *url = [NSURL URLWithString:imageUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
cell.imageView.image = [UIImage imageWithData:data];
How should i fetch these images without slowing down application launch (where should i put my code)?
I read a few things about NSOperation, is this the right way to go?
Tnx.
you can implement lazy loading
try this out
http://kshitizghimire.com.np/lazy-loading-custom-uitableviewcell/
or you can check apple's example
http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009394
good luck
Create class to download the Images. After downloading the Image you can Implement a delegate method. In the Delegate method Reload the cell with corresponding images.

How to load image from remote server on the UIImageView in iphone?

I am trying to load image from a remote server on the UIImageView in my application. Is it possible to load image from a remote server. I am using the following code
id path = #"http://upload.wikimedia.org/wikipedia/commons/c/c7/Sholay-Main_Male_Cast.jpg";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
I found this code sippnet on the net, but I dont understand how do I load this image on the ImageView.
Can anyone tell me how to add image on the UIImageView.
Thanx in advance...
you got almost all the way there... now just
imageView.image = img;
note that using dataWithContentsOfURL will block your user interface while the image loads, which will work if you're loading small images at startup, but is generally a bad idea; look into NSURLConnection.

caching the webcontent ( images ) on the iPhone

I have a question concerning the caching of webcontent.
I've created a UIWebview component
Code:
NSString *urlAddress = #"http://192.168.55.101/~test/mobile/iphone/ads/v0.1/";
//URL OBJECT footer
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320 , 100)];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[aWebView loadRequest:requestObj];
This shows a picture in my iPhone.
I've tried looking into cache attributes and functions (for instance NSURLRequestReturnCacheDataElseLoad ) to see which options I have , but what's the best way to cache the image appearing in this screen.
Or how do I use NSURLRequestReturnCacheDataElseLoad?
There is a good sample code:
Apple sample code
Description:
CacheInfo allows you to choose a URL, create a connection, load the resource asynchronously, and observe results such as the data size, load time, and cache usage. You can adjust the sizes of the shared memory and disk caches and observe how caching is affected during the load.