At the moment, I have a goal of writing an application that contains a UIImageView. Upon loading or pressing a button, an image from a web server will load into the view.
What is the simplest way to do this? I'm assuming using the API described here? https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
In the mean time, I will continue to try to get this working on my own. Any advice would be appreciated, especially if you have written code like this before.
Yes, this can be done by making a request through an NSURLConnection.
You might be interested in checking out SDWebImage. This library includes a category that adds a method to UIImageView that makes asynchronously loading an image from a remote URL as simple as this:
[self.imageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
Related
Could any one please tell me the best image cache available for loading numerous images in UITableviewcell?
Currently i am using SDWebimageCache but it seems it is creating problems in IOS9 particularly in iPad where the table scrolling makes the app to crash.
Help me with SDWebimagecache alternative.
The class which i used from SDWebImageCache is:
[cell.imgMain sd_setImageWithURL:imageLoadUrl
placeholderImage:[UIImage imageNamed:#"*placeholder image*"]
options:SDWebImageRefreshCached];
The class which i used from UIIMageView + AFNetworking is:
[cell.imgMain setImageWithURLRequest:[NSURLRequest requestWithURL:imageUrl]
placeholderImage:[UIImage imageNamed:#"wachizLogoIcon.png"]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
cell.imgMain.image=image;
}
failure:nil];
But still the application crashes and shows the following screen:
APPLICATION CRASHES IN IOS 9 and sometimes for IOS 8.4
Please help me from this situation. Lots of pressure ...
AFNetworking has a great UIImage category that does this well and works on iOS9. I too had issues with SDWebimageCache on iOS9.
Take a look at AsyncImageView.
AsyncImageView includes both a simple category on UIImageView for loading and displaying images asynchronously on iOS so that they do not lock up the UI, and a UIImageView subclass for more advanced features. AsyncImageView works with URLs so it can be used with either local or remote files.
Loaded/downloaded images are cached in memory and are automatically cleaned up in the event of a memory warning. The AsyncImageView operates independently of the UIImage cache, but by default any images located in the root of the application bundle will be stored in the UIImage cache instead, avoiding any duplication of cached images.
The library can also be used to load and cache images independently of a UIImageView as it provides direct access to the underlying loading and caching classes
You can use JMImageCache For lazy loading of image in UITableView
I want to load multiple images when my app starts from a website (i.e. all images in http://hello.com/images/ which are named 1.png, 2.png, 3.png..) , so that the images can be used anywhere in the program without needing to reload them every time I want to access them.
Can I simply create a class that holds a static NSArray and fill it at the beginning, to then create an instance of this class whenever I need the images or is there a better way to do it?
Right now, I am loading the images with the following code:
UIImage *image =[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://hello.com/images/%#.png,item]]]];
I want to make the app as efficient as possible, so I am concerned about the creation of multiple objects making it very demanding.
Thanks
You can try downloading the images asynchronously in a separate thread when the application starts
and use it later.
Here is the SO question and answer where the poster uses a custom class to download the images in the background asynchronously.
Try this for efficient download of images and UI also will not be blocked.
I am writing an app which is going to be displaying images found on my server in a UIImageView.
I need something that will asynchronously download the image and cache it while putting it in the UIImageView.
The download also needs to be able to be cancelled when I press a button.
Can anyone point me in the direction of something that can do this?
In the old times the framework for that was ASIHTTPRequest but is an abandoned project now. This https://github.com/AFNetworking/AFNetworking seems to be popular now.
Use the performSelectorInBackground:withObject: - method from NSObject ;)
Advise: The updating of the view must run on the main thread!
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsobject_Class/Reference/Reference.html
Is it possible to create a hidden UIWebView programmatically and then release it(destroy it completely deallocating all of it's memory) when I have used it? If so, could you give some tips. Thanks!
It seems odd that you want to use a hidden UIWebView object for some task when the main purpose of an UIWebView object is to present the html content. The answer to your question is yes. You can create them and remove them programmatically just like any other view as #Nil has mentioned but it seems your main purpose behind this is to get the content. If that's so, you have many other ways you can get that content without having to create a UIWebView object. You can use NSString's initWithContentsOfURL:encoding:error: method to get the contents of the url into a string which you can parse and use. NSData has a similar method. However to use these you will have to perform them in the background otherwise they will block the main thread. The most obvious and better way would be to use NSURLConnection or its popular counterpart ASIHTTPRequest.
Yes, you can create and destroy a uiwebview as any other uiview.
I'm loading a very big file in an NSimage with this code :
[[NSImage alloc] initWithContentsOfFile:aFile]
This operation take a few time. I want to display the loading status on my UI.
How can it's possible to read or calculate a progression ?
Thanks.
Initialize the image by referencing the file, which will not load it immediately. Then, set yourself as the image's delegate and respond to the incremental-loading messages that are part of the NSImageDelegate protocol. Then, attempt to ask the image for some information about itself (asking for its representations would probably be a good way), to cause the image to start loading.
I think this will still block your UI, though: You'll be able to display progress, but not to enable the user to work on other things while the image loads. I'm not sure how you would do that.