I have some code in which I have declared 14 images to be displayed in UIImageview and named them as 1,2,3,....14.jpg. These images are in a folder copied from desktop and placed in the project.
Now I have a server and client code running from which the images named 1,2,.... will be refreshed and new images will be coming. But when I try to execute my code can see the images which came for the first time and for every repetition there will be no change in images shown in simulator....
NSArray *array1=[NSArray arrayWithObjects:[UIImage imageNamed:#"1.jpg"],[UIImage imageNamed:#"2.jpg"],[UIImage imageNamed:#"3.jpg"],[UIImage imageNamed:#"4.jpg"],[UIImage imageNamed:#"5.jpg"],[UIImage imageNamed:#"6.jpg"],[UIImage imageNamed:#"7.jpg"],[UIImage imageNamed:#"8.jpg"],[UIImage imageNamed:#"9.jpg"],[UIImage imageNamed:#"10.jpg"],[UIImage imageNamed:#"11.jpg"],[UIImage imageNamed:#"12.jpg"],[UIImage imageNamed:#"13.jpg"],[UIImage imageNamed:#"14.jpg"],nil];
[UIImageView beginAnimations:Nil context:NULL];
image1.animationImages=array1;
image1.contentMode=UIViewContentModeScaleAspectFill;
image1.animationDuration=10;
image1.animationRepeatCount=10;
[image1 startAnimating];
When this code runs I am getting the same set of 14 images which are shown for the first time. It's not getting changed even though the images are changed in that folder.
The documentation for imageNamed: says it looks in the application’s main bundle. Since you can't write to the application bundle on a device, you're not going to be able to replace things that are distributed with the app if you load them that way.
You may want to switch to imageWithContentsOfFile: and move your distributed images to a writable location when the app is first run so that you can update them.
(You don't say where you're storing the images that you get from your server.)
Related
I've got magazine app and I want to know If is there any way how to preload UIView and UIImages?
My views structure:
MagazineView
-> pageView
-> imageContainerView
-> image
-> imageContainerView
-> image
-> textView
-> pageView
etc...
So my question is - How to preload images before views are visible? I want to make some Cache with 3 or 5 pages and makes threads for loading views. Any ideas?
I presume that to 'preload images' means downloading them before they are actually used so there is no delay when displaying them.
What you could do is download the images first using [NSData dataWithContentsOfURL:], then either dump the data into a file using writeToFile:atomically: on your NSData instance, or simply keep the data in heap in the NSData for future reuse.
Then when you need it, you can create UIImage using either [UIImage imageWithContentsOfFile] or [UIImage imageWithData:].
Note that if you save the cache to files, save those files in the cache folder, not in the documents folder, or mark them with the attribute that makes them skipped when syncing to iCloud, otherwise your app will be rejected by Apple.
Also keep in mind that downloading those images can taken long, timeout, or fail. You should do all this in a background thread in a way that does not block the main thread, have a visual indicator that the application is not frozen and have a fallback for the case where the downloads fail or the cache is flushed (if you save the images to files)
After you load images as J_D described, just put them into pageView and set them Hidden to YES. Every time you want to switch page, just call some method, that will pre-load new images and show those, which are already downloaded (set Hidden to NO) ;-).
Hope this will help you to solve your problem...
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.
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"]];
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.
Just started exploring Cocoa so pretty much a total noob.
I've written a very simple game. Set it up in Interface Builder and got it working fine.
It contains a number of buttons and I'm now trying to get the buttons to display images.
To start with I'm trying to get an image displayed on just one of the buttons which is called tile0 .
The image file (it's nothing but a green square at the moment, but I'm just trying to get that working before I attempt anything more exotic) is sitting in the same directory as the class file which controls the game.
I have the following code sitting in my wakeFromNib method:
NSString *myImageFileName = [[NSString alloc] init];
myImageFileName = #"greenImage.jpg";
NSImage *myImage = [[NSImage alloc] initByReferencingFile:myImageFileName];
[tile0 setImage: myImage];
Trouble is, the game runs fine, but the image isn't appearing on my button.
Is there someone who could kindly tell me if I'm doing something obviously wrong?
Many Thanks.
The first problem is you allocate a string but then replace it with a string constant. That isn't causing your image problem but it is a memory leak.
I've never used initByReferencingFile, I usually just use imageNamed:. There is an example of using imageNamed here.