开发者

adding multiple images programmatically in iPhone

开发者 https://www.devze.com 2023-02-18 23:29 出处:网络
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 co

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 开发者_JAVA技巧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.

0

精彩评论

暂无评论...
验证码 换一张
取 消