adding multiple images programmatically in iPhone - iphone

I want to add around 80 imageview on my view and then later on want to change their images very frequently(multiple times per second, working on an audio meter). I have added images using following code:
-(void)drawAudioMeter {
UIImageView *imgvw = nil;
int x = 1;
for(int i = 0; i<80; i++) {
imgvw = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"line_dull.png"]];
imgvw.tag = i+100;
imgvw.frame = CGRectMake(x, 400, 3, 30);
x = x + 4;
[self.view addSubview:imgvw];
[imgvw release];
}
}
Now how will I change the images in the imageviews as I am not having any name for the imageviews. Using a for loop every second on all the images does not look as feasible option to me. Can some one point me out how should I do it?

You can use viewWithTag to get a specific image by its tag
UIIMageView *image = [UIView viewWithTag:1];
viewWithTag: Returns the view whose
tag property contains the specified
integer value.
(UIView *)viewWithTag:(NSInteger)tag Parameters tag The tag value to search
for. Return Value The view in the
receiver’s hierarchy whose tag
property matches the value in the tag
parameter.
Discussion This method searches the
current view and all of its subviews
for the specified view.
Availability Available in iOS 2.0 and
later. See Also #property tag
Related Sample Code GenericKeychain
SeismicXML Declared In UIView.h
taken from: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html

Create array of UIImageView's in your .h file
UIImageView* imgvw[80];
Create the imgvw's in a forloop in viewDidLoad.
And remove creating and releasing of image views in your for loop. And so, in your drawAudioMeter, you can just get the object of your image view in the for loop and change the images accordingly.

I think it's not good idea to draw audio meter via 80 UIImage's ... Maybe you should look at SpeakHere example from Apple how they do this. Open Xcode, open help (option-cmd-?) and search for SpeakHere. It's an example which does record / playback audio with audio meter.

Related

How to Open the Image by clicking on the thumbnail in the order the image are saved in the NSArray?

In my app i have thumbnail view and i want the user to go to the next view with the imageview and show the particular thumbnail clicked.But this image view is attached to the UIScroll with paging and UIPagecontrol. So i want the specific image to be open and at the specific index so that the user can swipe left or right to see other images.
This needs to be exactly like iPhoone's Photo app.
Please give me some link or tutorial to follow this. I can pass the index and open the particular image but just not able to set the image in order.
Thanks,
#Ashutosh
I am sure following steps might be useful to you.
1] Pass the index of image clicked along with the entire data source (Array of images) to the detail screen.
2] Generate your scrollview with defined Rects for each image along with the paging.
3] Generate the rect for the previously selected index and make your scroll view to scroll to that specific rect, accordingly update your page control to show the desired page.
4] Make a method which will update the page control as user swipes through the scroll view image gallery and vice versa , i.e when page control is clicked update your scroll view.
Hope It helps!!
To expand a little here is some code that will help you accomplish some of the steps suggested by #Rahul Sharma:
//You can first create an array that holds your images that will be in the UIScrollview.
NSArray *images = [NSArray arrayWithObjects: [UIImage imageNamed:#"...],nil];
//In this example images is and array of UIImages and imgScroll is your UIScrollView
This will create the Scrollview with UIImageviews
for (int i = 0; i < [images count]; i++){
CGRect frame;
frame.origin.x = self.imgScroll.frame.size.width * i;
frame.origin.y = 0;
frame.size = CGSizeMake(self.imgScroll.frame.size.width, self.imgScroll.frame.size.height);
UIImageView *subView = [[UIImageView alloc] initWithFrame:frame];
subView.image = [frames objectAtIndex:i];
subView.contentMode = UIViewContentModeScaleAspectFit;
[self.imgScroll addSubview:subView];
[subView release];
}
self.imgScroll.contentSize = CGSizeMake(self.imgScroll.frame.size.width * images.count, self.imgScroll.frame.size.height);
[self.imgScroll setDelegate:self];
Then call the delegate method to know when your imgScroll has scrolled
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
Then using the index of the images array that you want to scroll to, you can
calculate the offset of that UIImageView and set the scrollview to scroll
to that rect when the thumbnail is clicked.
[imgScroll scrollRectToVisible:imgToDisplay //This is a CGRect// animated:NO];
Hope this helps
Tams

How to change the icon of an image to represent it has been selected on an iPhone

I am trying to simulate the same behaviour as the defaults "Photos" application found on the iPhone OS 3.X-4.0.
Basically when the user selects multiple photos in the Album via the UIImagePickerController from the Image Library,an icon (tick mark) appears on the image to represent that the image has been selected by the user. How to implement this functionality.
Many thanks
create another UIImageView with the tickmark-image and add it as a subview to your selected UIImageView. Like this:
- (void)putCheckmarkOnImageView:(UIImageView *)anImageView {
UIImageView *newIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"checkmark.png"]];
newIV.tag = 42;
CGPoint newIVPosition = CGPointMake(anImageView.bounds.size.width - newIV.bounds.size.width,
anImageView.bounds.size.height - newIV.bounds.size.height);
CGRect newFrame = newIV.frame;
newFrame.origin = newIVPosition;
newIV.frame = newFrame;
[anImageView addSubview:newIV];
[newIV release];
}
- (void)removeCheckmarkOnImageView:(UIImageView *)anImageView {
[[anImageView viewWithTag:42] removeFromSuperview];
}
[_arrayOfCheckedPhotos addObject:anImageView.image]; // Same code, but add it to a mutable array
// this is assuming you want the array of images
Just put that line of code before the release in the comment above mine^ than later you can access all of the selected images, make the array a property so its easily accessible
and also in the removeCheckMark function say:
[_arrayOfCheckedPhotos removeObjectAtIndex:

How to check image during animation

I have set up an animation in the following way (self is an UIImageView, myImages an Array of UIImages):
self.animationImages = myImages;
self.animationDuration = 50;
self.animationRepeatCount = 0;
[self startAnimating];
During the animation I'd like to check the current image. I tried it the following way
if([self image]==[UIImage imageNamed:#"image1.png"]);
but this does not work. Is there a straight forward way for this? Can I keep track of which image is shown during the animation?
There is no official way to get the currently displayed image (index) from an animated UIImageView. Note: If the animationImages property contains a value other than nil, the contents of the image property is not used at all and will not contain usable data.
Still, you can measure the time that has passed since the animation started and evaluate the current frame/image index yourself.

3D Rotation of Object

I am trying to rotate my object like shakes dice.
please suggest simplest way to implement it in my iphone application.
Any kind of sample code or documentation.
http://www.amazon.com/OpenGL-SuperBible-Comprehensive-Tutorial-Reference/dp/0321498828
If you want to do this without using OpenGL you can use a UIImageView and the set a series of animation images. By creating a series of images you can create a "rolling dice" effect. Here is an example of how to do this:
// create a UIImageView
UIImageView *rollDiceImageMainTemp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"rollDiceAnimationImage1.png"]];
// position and size the UIImageView
rollDiceImageMainTemp.frame = CGRectMake(0, 0, 100, 100);
// create an array of images that will represent your animation (in this case the array contains 2 images but you will want more)
NSArray *savingHighScoreAnimationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"rollDiceAnimationImage1.png"],
[UIImage imageNamed:#"rollDiceAnimationImage2.png"],
nil];
// set the new UIImageView to a property in your view controller
self.viewController.rollDiceImage = rollDiceImageMainTemp;
// release the UIImageView that you created with alloc and init to avoid memory leak
[rollDiceImageMainTemp release];
// set the animation images and duration, and repeat count on your UIImageView
[self.viewController.rollDiceImageMain setAnimationImages:savingHighScoreAnimationImages];
[self.viewController.rollDiceImageMain setAnimationDuration:2.0];
[self.viewController.rollDiceImageMain.animationRepeatCount:3];
// start the animation
[self.viewController.rollDiceImageMain startAnimating];
// show the new UIImageView
[self.viewController.view addSubview:self.rollDiceImageMain];
You can modify the creation and setup including the size, position, number of images, the duration, repeat count, etc as needed. I've used this feature of UIImageView to create simple animation effects and it works great!
Please let me know if you try this and if it works for your needs. Also, post any follow up questions.
Bart
nice your example, but do you think it's possible to rotate / control the rotation with touch gesture ?

Setting up multiple UIImageView instances

I would like to create a 4 x 6 grid of UIImageViews that each contain a slightly different image. I would also like to be able to randomly select one of the instances and change it's image.
My question is what's the best way to set up the UIImageViews in a grid formation, perform a few actions between each setup, and randomly pick 1 of the 24 instances once setup is complete. Optimally, I wouldn't have to set up each one by one.
Thanks in advance.
There are different approaches you can take, depending on whether or not you want to use Interface Builder to layout your grid.
One option is to layout your 24 UIImageViews as subviews of a common parent view within IB. On the View Attributes tab you can set a "Tag" number from 1 to 24 to differentiate your UIImageViews. Then in your code you can use [parentView viewWithTag:tagNumber] to access each UIImageView.
If you prefer to do things more programmatically, you could create each of your UIImageViews in a loop in the loadView method of your UIViewController subclass. You could maintain an array (or an array of arrays corresponding to rows and columns) as a property of your controller, and store a reference to each of these image views as you create them. For each UIImageView you create, set its imageView.frame property to define its position, then call [view addSubview:imageView] to add it to the parent view.
I would do it programmatically for your sake.
NSArray *myViews = //I assume you can create an array of views
for (int i=0; i<rows; ++i) {
for (int j=0; j<columns; ++j) {
UIImageView *thisImageView = [myViews objectAtIndex:(i*columns+j)];
CGSize size = thisImageView.image.size;
[thisImageView setFrame:CGRectMake(j*size.width, i*size.height, size.width, size.height)];
[self.view addSubview:thisImageView];
}
}
//Later to pick one randomly
UIImageView *myRandomView = [myViews objectAtIndex:(arc4random()%[myViews count])];
[myRandomView setImage:myNewImage];