Dynamically creating a uiImageView - iphone

I'm still very new to cocoa touch so please excuse any terminology that I may have got wrong.
I have a bunch of images in my bundle consecutively named image0.png, image1.png etc...
I have a picker in a viewcontroller and an instance variable that keeps track of the current row.
When a user clicks a button I want to be able to create a uiImageView object (and first test whether the view already exists) based on the row number, ie: uiImageView *imageView1 for row1.
Is this possible or would it just be easier to create a line of code for each possible case?
I hope this makes sense, and thanks in advance for anyone that can shed any light!

Why not just create one UIImageView in Interface Builder and then swap out the image it's displaying depending on your picker index?
Implement:
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
[imageView setImage:[imagesArray objectAtIndex:row]];
}
Where imageView is an instance variable (IBOutlet) connected to the image view control in Interface Builder.

Ok, that makes sense.
I think what I was trying to do was take lazy loading to the extreme by only creating the uiImage object and the uiImageView object the first time it was called on from the picker (having clicked the "go" button).
I anticipate having around 40 images that will fill the screen.
Programatically, would this be an acceptable method for this number of images?
Thanks very for the responses guys.

Related

How to populate an image in UITableViewCell even if user scroll the table

I'll try to explain my self, I have ContactsViewController that shows a table view with a list of contacts (the model is an array of Contact objects), each cell display an image of a contact.
Currently what I do to populate the cell's UIImageView is this:
1. I override the Contact image property getter -
- (UIImage *)contactImage
{
if (!_contactImage) {
_contactImage = [[UIImage imageNamed:#"placeHolder.png"] retain];
[self asyncDownloadContactImageFromServer];
}
return _contactImage;
}
Then when I finish downloading the image I set it to the contactImage property and I post a ContactUpdatedImageNotification.
My ContactsViewController then get this notification and reload the cell of this contact, this will set the downloaded image to the cell's imageView.
The result of this is good async fetching of the images without blocking the UI while the user scroll the table view.
BUT there is something small that bothers me, when the a user scroll the table view and reveal new cells the new cell's image get download as expected but the cell's imageView is not updated with the new downloaded image till the user pick up his finger.
I supposed that I need to do something in another thread to make this effect, but I don't know how?
The image is not updated until the user stops scrolling due the code being executed in the default runloop, which gets delayed until scrolling finishes. This other question deals with the difference between the runloops, NSDefaultRunLoopMode vs NSRunLoopCommonModes and it precisely recommends not updating the images while scrolling since that can introduce jerkiness in the scrolling itself if you are not careful.
Also, now that you know about the existence of these runloop modes you will be able to find much more information about them in the xcode documentation or internet.
Hey Eyal visit following url...you will get answer and as well sample code...
tableview with different cell with different images
Hope this will help you...

How to dynamically add a UIView

Hello I want to have a table of UILabel. The first column is the name of an item and the second is the value of that item. The problem is I don't know how many rows I need until the user logins in. I know how to add rows to a table in Android however it seems impossible to do with the iPhone. How would I achieve this?
Consider using a UITableView with custom cells.
The advantage is that, depending on your data source, a table view can automatically update when your data changes. An event-driven architecture (ie: update table when data is available/changes) is in-line with objective-c land.
For examples of custom cells: http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
For tutorials on making a UITableView backed by a data source, I'd point you to the excellent Stanford iOS programming course, available on iTunes U.
There is a view called UITableView special for these aims. Read this article:
Table View Programming Guide for iOS
UITableView Class Reference
You need to implement the UITableViewDataSource protocol. In this case, one method of the protocol is this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [myData count];
}
You can see the entire protocol documentation here:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html
You can do it in two different ways.
In the first one you can create labels programatically. A label (UILabel class) inherits from UIView. So, if you have a superview, you can add labels on it depending on user login. You have to position labels using the frame property. For example:
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 40, 300, 50)];
titlelabel.text = #"title";
[ySursuperview addSubview:titleLabel];
[titleLabel release];
// add labels here
In the second case you could use a UITableView (that contains an header view for the title) and a where each UITableViewCell contains NameX and ValueX. You could use also a custom UITableViewCell.
In my opinion the second solution could be more elegant and easier to maintain. The first one, instead, is more quickly but you have to do some calculations to obtain the grid layout you want.

Why not UIImage itself but UIImage view

I'm learning to develop apps for Iphone. I follow a book by Apress which I find very useful. But as nothing is perfect some issues are not well described and just skipped.In one of the applications I have to assign five images to each of the five components of a pickerview. But my question is why do/can not we use an instance of UImage itself but UIImageView to display on the picker.
As in above question you are asking why we can not use UIImage instead of UIImageView as component for UIPickerView.
UIImage is subclass of NSObject class, If we talk in terms of M-V-C its a M(model). and model in itself is nothing until its used.
UIImageView on the other hand is subclass of UIView which stands for V(view) in M-V-C so it uses your model for its contents.
So, they are two different things, not alternate. Also, if you go through UIPickerView's class documentation then you will find that it has method
- (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component
that configures view for specified row component. so it meanse we need view(UIImageView) and not the model class(UIImage). here for component you can return subclass of UIView.
Please refer apple's documentation.
Thanks,
You need a frame to place your photo, similarly You need UIImageView as place holder to place UIImage.
UIImage is just an Image loaded from file. View is able to handle touch events and only Views are displayed.

How do I get the items to refresh in the Flowcover example?

The Flowcover example code provides an OpenGL ES implementation of the coverflow effect. How can I refresh the images used in that example?
I have tried [view reloadData]; but that didn't refresh the images. What else should I do?
Could you elaborate a bit on where you want to refresh the images? Are you reloading a new set of images you want to display?
Note that there is a cache in the class FlowCover that you might need to empty. There is currently no interface to do just that so you might want to add the next method to the class DataCache:
- (void) emptyCache {
[fDictionary removeAllObjects];
[fAge removeAllObjects];
}
Remember to send setNeedsDisplay to the View after modifying any data it depends on.

Animating UIPickerView

Hi I am developing an iPhone app which uses pickerview, Upon shaking app selects one random choice. Everything works well but I was wondering is there any way to animate the UIPIckerView as Urban Spoon has done it.
Confession time: when I posted the other answer, I hadn't ever used Urban Spoon, and I assumed you were being lazy. I'm not overwriting my earlier answer, because such a rude response deserves the downvotes it got.
If I were going to make an animation like the ones on urban spoon's picker wheels, I'd probably use UIImageView's flipbook-style animation, as demonstrated here: http://iosdevelopertips.com/graphics/animated-gif-animated-images-iphone-style.html
Short version: the property UIImageView.animationImages can be loaded with an NSArray of UIImages. You can set properties like .animationRepeatCount and .animationDuration. And then [UIImageView startAnimating] will make that thing start animating.
I'd guess Urban Spoon does that for a second with the "fast rolling" set of images, replaces that with the "medium-speed rolling" set of images, and then gets rid of the UIImageView entirely and replaces it with a UIPickerView that's animating its way to a pre-selected random position.
The method you want is
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
All else fails, you could always try looking at the docs. http://developer.apple.com/iphone/library/documentation/uikit/reference/UIPickerView_Class/Reference/UIPickerView.html