iPhun

iPhun


  • Name: [not set]
  • Favorite Languages: [not set]
  • Website: [not set]
  • Location: [not set]
  • About Me: [not set]

Recent Comments

  • Loading Images Asynchronously on iPhone using NSInvocationOperation
    06/30/2010 - 15:43

    This is a great tutorial!! I have a question. I have a quiz type app that I am trying to load the images asynchronously through resource files and I can't quite get it to work without loading all the images? It takes too much memory to load all the images (i'm aware of that), but how can I release the images from the superview without causing issues with the random integers? Here's the file - Any help is greatly appreciated!!

    - (void)viewDidLoad {
           
           
            index=16;
            var_Index=0;
           
           
            X=160;
            R=80;
            W1=240;
            W2=80;
            W3=240;
            rgt_ans=60;
            wrg_ans=60;
           
           
           
            XC=80;
            YC=80;
            XW1=240;
            YW1=80;
            XW2=80;
            YW2=130;
            XW3=240;
            YW3=130;
           
            time=15;
           

           
           
           
            [self KBC_Game];
           
        [super viewDidLoad];
    }

    -(void)KBC_Game{
           
           
            currentViewLocation = 250;
            var_Score=0;
            Questions_Count=0;     
           
           
           
           
           
            KBC_Questions=[[NSMutableArray alloc]init];
            KBC_Image_Ans=[[NSMutableArray alloc]init];
            KBC_Image_Wrong1=[[NSMutableArray alloc]init];
            KBC_Image_Wrong2=[[NSMutableArray alloc]init];
            KBC_Image_Wrong3=[[NSMutableArray alloc]init];
           
           
                    img_Bankground =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"BarQuizGameBackground.png"]];
                    [img_Bankground setUserInteractionEnabled:YES];
                    [self.view addSubview:img_Bankground];
           
                    imageView_tmp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"prize%d.png"]];
                    [imageView_tmp setFrame:CGRectMake(60, 200, 200, 100)];
                    [imageView_tmp setHidden:NO];
                    [imageView_tmp setImage:[UIImage imageNamed:@"115.png"]];
                    [self.view addSubview:imageView_tmp];
                    [imageView_tmp release];
           
           
                    scrol_KBC=[[UIScrollView alloc]initWithFrame:CGRectMake(0,320,320,160)];
                    scrol_KBC.pagingEnabled = YES;
                    scrol_KBC.delegate = self;
                    scrol_KBC.scrollEnabled = YES;
                    [scrol_KBC setBackgroundColor:[UIColor blackColor]];
                    [scrol_KBC setContentSize:CGSizeMake(scrol_width*16,scrol_height)];
                    [scrol_KBC setDirectionalLockEnabled:YES];
                    [self.view addSubview:scrol_KBC];
           
           
            img_Bankground =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"background.png"]];
            [img_Bankground setUserInteractionEnabled:YES];
            //[scrol_KBC addSubview:img_Bankground];
            [img_Bankground release];
           

    HERE IS MY PROBLEM
            for(int i=1;i<45;i++){
                                   
            {                      
                   
                    [KBC_Questions addObject:[UIImage imageNamed:[NSString stringWithFormat:@"KBCQ%d.png",i]]];
                    [KBC_Image_Ans addObject:[UIImage imageNamed:[NSString stringWithFormat:@"KBCC%d.png",i]]];            
                    [KBC_Image_Wrong1 addObject:[UIImage imageNamed:[NSString stringWithFormat:@"KBCW%d.png",i]]]
                    [KBC_Image_Wrong2 addObject:[UIImage imageNamed:[NSString stringWithFormat:@"KBCWW%d.png",i]]];
                    [KBC_Image_Wrong3 addObject:[UIImage imageNamed:[NSString stringWithFormat:@"KBCWWW%d.png",i]]];
                   
            }      
           
            }
           
            arr_Index=[[NSMutableArray alloc]init];
           
           
            [self random_value];
            NSLog(@"arr_Index is %@",arr_Index);
           
            //NSLog(@"size is %d ",[KBC_Questions count]) ;
            //NSLog(@"array contents is %@ ",KBC_Questions );
            for(int i=0;i<16;i++)
            {
                   
                   
                    //SWAPING ANSWERS
                    int rand_value=arc4random()%3; 
                    NSLog(@"rand_value is%d",rand_value);
                   
                    if(rand_value==0)
                    {
                           
                           
                            int swap;
                            swap=XC;
                            XC=XW3;
                            XW3=swap;      
                           
                           
                           
                            swap=YC;
                            YC=YW3;
                            YW3=swap;
                           
                    }
                   
                   
                    if(rand_value==1)
                    {
                            int swap;
                            swap=XC;
                            XC=XW1;
                            XW1=swap;      
                           
                           
                           
                            swap=YC;
                            YC=YW1;
                            YW1=swap;                      
                    }              
                   
                   
                   
                    else if(rand_value==2)
                    {
                            int swap;
                            swap=XC;
                            XC=XW2;
                            XW2=swap;      
                           
                           
                           
                            swap=YC;
                            YC=YW2;
                            YW2=swap;                      
                    }              
                   
                   
                    /*      {
                     UIImageView    *imageView;
                     
                     // create game begin view
                     imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"LetsBegin.png"]];
                     imageView.center = [self view].center;
                     imageView.alpha = 0.0;
                     imageView.transform = CGAffineTransformMakeScale (6.0, 6.0);
                     [[self view] addSubview:imageView];
                     
                     // animate it in
                     [UIView beginAnimations:nil context:nil];
                     [UIView setAnimationDuration:ANIM_LONG];
                     imageView.alpha = 1.0;
                     imageView.transform = CGAffineTransformIdentity;
                     [UIView commitAnimations];
                     [imageView release];  
                     
                     // animate it out
                     [UIView beginAnimations:nil context:nil];
                     [UIView setAnimationDuration:ANIM_LONG];
                     imageView.alpha = 0.0;
                     imageView.transform = CGAffineTransformMakeScale (3.0, 3.0);
                     [UIView commitAnimations];
                     
                     }
                     */

                    /*
                     {
                     CGAffineTransform moveTransform = CGAffineTransformMakeTranslation(-320, 0);
                     CABasicAnimation *moveAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
                     moveAnim.duration = 2.0;
                     moveAnim.toValue= [NSValue valueWithCATransform3D:
                     CATransform3DMakeAffineTransform(moveTransform)];
                     moveAnim.repeatCount = 3;
                     [plane2.layer addAnimation:moveAnim forKey:@"animateTransform"]
                     }
                     */

                   
                   
                            //NSLog(@"i value is %d",i);
                            UIImageView *img_Question=[[UIImageView alloc]initWithImage:[KBC_Questions objectAtIndex:[[arr_Index objectAtIndex:i]intValue]]];
                            //[img_Question setBackgroundColor:[UIColor redColor]];
                            //img_Question.tag=i;
                            [img_Question setCenter:CGPointMake(X,23)];      
                            [scrol_KBC addSubview:img_Question];
                            [img_Question release];
                       
                            X=X+320;
                   
                   
                            btn_right=[UIButton buttonWithType:UIButtonTypeCustom];
                            [btn_right setFrame:CGRectMake(0,0, 125,35)];
                            btn_right.tag=i;
                            [btn_right setBackgroundColor:[UIColor redColor]];
                            //[btn_right setCenter:CGPointMake(var_x_origin_image_right,var_y_origin_image_right)];  
                            [btn_right setCenter:CGPointMake(XC,YC)];
                            //[btn_right setImage:[KBC_Image_Ans objectAtIndex:[[arr_Index objectAtIndex:i]intValue]] forState:UIControlStateNormal];
                            [btn_right setImage:[KBC_Image_Ans objectAtIndex:[[arr_Index objectAtIndex:i]intValue]] forState:UIControlStateNormal];
                            [btn_right addTarget:self action:@selector(right_ans:) forControlEvents:UIControlEventTouchUpInside];
                            [scrol_KBC addSubview:btn_right];
                           
                   
                            XC=XC+320;
                   
                   
                   
                           
                            btn_wrong1=[UIButton buttonWithType:UIButtonTypeCustom];
                            [btn_wrong1 setFrame:CGRectMake(0,0, 125, 35)];
                            btn_wrong1.tag=i;
                            [btn_wrong1 setBackgroundColor:[UIColor redColor]];
                            [btn_wrong1 setCenter:CGPointMake(XW1,YW1)];   
                            //[btn_wrong1 setImage:[KBC_Image_Wrong1 objectAtIndex:[[arr_Index objectAtIndex:i]intValue]] forState:UIControlStateNormal];
                            [btn_wrong1 setImage:[KBC_Image_Wrong1 objectAtIndex:[[arr_Index objectAtIndex:i]intValue]] forState:UIControlStateNormal];
                            [btn_wrong1 addTarget:self action:@selector(wrong_ans:) forControlEvents:UIControlEventTouchUpInside];
                            [scrol_KBC addSubview:btn_wrong1];
                           
                            XW1=XW1+320;
                   
                   
                            btn_wrong2=[UIButton buttonWithType:UIButtonTypeCustom];
                            [btn_wrong2 setFrame:CGRectMake(0,0, 125, 35)];
                            btn_wrong2.tag=i;
                            [btn_wrong2 setBackgroundColor:[UIColor redColor]];
                            [btn_wrong2 setCenter:CGPointMake(XW2,YW2)];   
                            //[btn_wrong2 setImage:[KBC_Image_Wrong2 objectAtIndex:[[arr_Index objectAtIndex:i]intValue]] forState:UIControlStateNormal];
                            [btn_wrong2 setImage:[KBC_Image_Wrong2 objectAtIndex:[[arr_Index objectAtIndex:i]intValue]] forState:UIControlStateNormal];
                            [btn_wrong2 addTarget:self action:@selector(wrong_ans:) forControlEvents:UIControlEventTouchUpInside];
                            [scrol_KBC addSubview:btn_wrong2];
                           
                           
                            XW2=XW2+320;
                   
                   
                        btn_wrong3=[UIButton buttonWithType:UIButtonTypeCustom];
                            [btn_wrong3 setFrame:CGRectMake(0,0, 125, 35)];
                            btn_wrong3.tag=i;
                            [btn_wrong3 setBackgroundColor:[UIColor redColor]];
                            [btn_wrong3 setCenter:CGPointMake(XW3,YW3)];
                            //[btn_wrong3 setImage:[KBC_Image_Wrong3 objectAtIndex:[[arr_Index objectAtIndex:i]intValue]] forState:UIControlStateNormal];
                            [btn_wrong3 setImage:[KBC_Image_Wrong3 objectAtIndex:[[arr_Index objectAtIndex:i]intValue]] forState:UIControlStateNormal];
                            [btn_wrong3 addTarget:self action:@selector(wrong_ans:) forControlEvents:UIControlEventTouchUpInside];
                            [scrol_KBC addSubview:btn_wrong3];
                           
                            XW3=XW3+320;
                   
            }
           
           
           
    }



    -(IBAction)right_ans:(UIButton*)sender
    {
           
           
            Flag_Right_Button_Pressed=1;
            [btn_right setEnabled:YES];
           
            scrol_KBC.scrollEnabled = YES;
            NSLog(@"Auto_Scroll_Var %d",Auto_Scroll_Var,0);
           
            {
                    AVsound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"ding2" ofType:@"wav"]  ] error:NULL];
                    AVsound.numberOfLoops=0;
                    [AVsound play];
            }
           
           
            [imageView_tmp setHidden:NO];
           
           
            Prize_Count=Auto_Scroll_Var+1;
           
            if(Auto_Scroll_Var+=320)
                    scrol_KBC.contentOffset= CGPointMake(Auto_Scroll_Var,0);
           
           
            if(Auto_Scroll_Var==320)
            {
                    [imageView_tmp setImage:[UIImage imageNamed:@"215.png"]];
                   
                   
            }
           
            if(Auto_Scroll_Var==640)
            {
                    [imageView_tmp setImage:[UIImage imageNamed:@"315.png"]];
                   
            }
           
            if(Auto_Scroll_Var==960)
            {
                    [imageView_tmp setImage:[UIImage imageNamed:@"415.png"]];
                   
            }
           
            if(Auto_Scroll_Var==1280)
            {
                    [imageView_tmp setImage:[UIImage imageNamed:@"515.png"]];
                   
            }
           
            if(Auto_Scroll_Var==1600)
            {
                    [imageView_tmp setImage:[UIImage imageNamed:@"615.png"]];
                   
            }
           
            if(Auto_Scroll_Var==1920)
            {
                    [imageView_tmp setImage:[UIImage imageNamed:@"715.png"]];
                   
            }              
           
            if(Auto_Scroll_Var==2240)
            {
                    [imageView_tmp setImage:[UIImage imageNamed:@"815.png"]];
                   
            }                                              
           
            if(Auto_Scroll_Var==2560)
            {
                    [imageView_tmp setImage:[UIImage imageNamed:@"915.png"]];
                   
            }      
           
           
            if(Auto_Scroll_Var==2880)
            {
                    [imageView_tmp setImage:[UIImage imageNamed:@"1015.png"]];
                   
            }
           
            if(Auto_Scroll_Var==3200)
            {
                    [imageView_tmp setImage:[UIImage imageNamed:@"1115.png"]];
                   
            }
           
           
            if(Auto_Scroll_Var==3520)
            {
                    [imageView_tmp setImage:[UIImage imageNamed:@"1215.png"]];
                   
            }
           
           
            if(Auto_Scroll_Var==3840)
            {
                    [imageView_tmp setImage:[UIImage imageNamed:@"1315.png"]];
                   
            }
           
            if(Auto_Scroll_Var==4160)
            {
                    [imageView_tmp setImage:[UIImage imageNamed:@"1415.png"]];
                   
            }
           
            printf("Scrolview Locations>>>%d\n",Auto_Scroll_Var);
           
            if(Auto_Scroll_Var==4480)
            {
                   
                   
                    [imageView_tmp setImage:[UIImage imageNamed:@"1515.png"]];
                   
                    if(Prize_Count = 1){
                           
                            [OFAchievementService unlockAchievement:@"375154"];
                    }
                    if(Prize_Count > 10){
                           
                            [OFAchievementService unlockAchievement:@"376024"];
                    }
                   
                    if(Prize_Count > 25){
                           
                            [OFAchievementService unlockAchievement:@"376384"];
                    }
                   
                    if(Prize_Count > 50){
                           
                            [OFAchievementService unlockAchievement:@"376394"];
                    }
                    /*             
                     
                     
                     [OFAchievementService unlockAchievement:@"376404"];
                     */

                    {
                            UIImageView     *imageView;
                           
                            // create game over view
                            imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Brainiac.png"]];
                            imageView.center = [self view].center;
                            imageView.alpha = 0.0;
                            imageView.transform = CGAffineTransformMakeScale (6.0, 6.0);
                            [[self view] addSubview:imageView];
                           
                            // animate it in
                            [UIView beginAnimations:nil context:nil];
                            [UIView setAnimationDuration:ANIM_LONG];
                            imageView.alpha = 1.0;
                            imageView.transform = CGAffineTransformIdentity;
                            [UIView commitAnimations];
                           
                           
                    }
                   
                   
                   
                    UIAlertView *alert4 = [[UIAlertView alloc]
                                                               initWithTitle:@"You are a brainiac"
                                                               message:@"You Have Earned 1 Achievement Point"
                                                               delegate:self
                                                               cancelButtonTitle:@"OK"
                                                               otherButtonTitles:nil];
                   
                    [alert4 show];
                    [alert4 release];
                   
            }
           
            if(Auto_Scroll_Var==4840)
            {
                   
                    [imageView_tmp setImage:[UIImage imageNamed:@"1515.png"]];
            }
           

    }

    - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex{
           
            //[self.navigationController setNavigationBarHidden:NO];
           
            //[self.navigationController popViewControllerAnimated:YES];
           
            ScoreController *scoreController = [[ScoreController alloc] init];
            [self.navigationController pushViewController:scoreController animated:YES];
            [scoreController release];
           
    }

    -(IBAction)wrong_ans:(UIButton*)sender
    {
           
            if(!(Flag_Wrong_Button_Pressed))
                   
                   
            {
                   
                    NSLog(@"Auto_Scroll_Var %d",Auto_Scroll_Var);
                   
                    //[imageView_tmp setHidden:NO];
                    //if(Auto_Scroll_Var<5)
                    //if(!(Flag_Wrong_Button_Pressed))
                    {
                            [imageView_tmp setHidden:NO];
                                                   
                            if(Prize_Count>-1)
                                   
                            {
                                    UIImageView     *imageView;
                                   
                                    // create game over view
                                    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"GameOver.png"]];
                                    imageView.center = [self view].center;
                                    imageView.alpha = 0.0;
                                    imageView.transform = CGAffineTransformMakeScale (6.0, 6.0);
                                    [[self view] addSubview:imageView];
                                   
                                    // animate it in
                                    [UIView beginAnimations:nil context:nil];
                                    [UIView setAnimationDuration:ANIM_LONG];
                                    imageView.alpha = 1.0;
                                    imageView.transform = CGAffineTransformIdentity;
                                    [UIView commitAnimations];
                            }
                            {
                                    AVsound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"buzzer2" ofType:@"wav"]  ] error:NULL];
                                    AVsound.numberOfLoops=0;
                                    [AVsound play];
                                   
                                                                   

                            }
                           
                           
                            {
                                   
                                    NSLog(@"0");
                                    [imageView_tmp setImage:[UIImage imageNamed:@"%d"]];
                                   
                                    UIAlertView *alert = [[UIAlertView alloc]
                                                                              initWithTitle:@"Seriously?? Start Studying!!"
                                                                              message:@"Better Luck Next Time!"
                                                                              delegate:self
                                                                              cancelButtonTitle:@"OK"
                                                                              otherButtonTitles:nil];
                                    [alert show];
                                    [alert release];
                                   
                            }
                           
                    }
                   
            }
    }


                   
    -(void)random_value
    {        
            int randomIndex = [self getRandomNumber:0 to:19];      
           
            for (int c = 0; c < [arr_Index count];c++)
                   
            {
                   
                    //      printf("no of times execute....\n%d",c);
                    if([[arr_Index objectAtIndex:c] intValue]==randomIndex)
                    {              
                            randomIndex=0
                    }
            }
           
            if (randomIndex==0)
            {
                    [self random_value];
            }
           
            else
            {
                   
                    if(var_Index<16)
                    {
                            var_Index++;                   
                            [arr_Index addObject:[NSNumber numberWithInt:randomIndex]];                    
                            [self random_value];
                    }
                   
                    else
                    {
                            //printf("Random Value.....\n\n");
                            //for(int i=0;i<[arr_Index count];i++)
                            //{
                            //       printf("%d\n",[[arr_Index objectAtIndex:i] intValue]);
                            //}    
                           
                            return ;
                    }
            }
    }


    //--------Random Number Manipulation---------
    - (int)getRandomNumber:(int)frm to:(int)to
    {
           
            return (int)frm + arc4random() % (to-frm+1);
    }




    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{       
            NSLog(@"hie");
            Flag_Right_Button_Pressed=0;
           
                   
            //X_Axis_ScrollView=X_Axis_ScrollView+320;
            Auto_Scroll_Var=Auto_Scroll_Var+=320;
           
            NSLog(@"Auto_Scroll_Var is %d",Auto_Scroll_Var);
            NSLog(@"scrol_KBC.contentOffset.x is %f",scrol_KBC.contentOffset.x);   
           
            scrol_KBC.scrollEnabled = NO;
           
            [scrol_KBC setDirectionalLockEnabled:YES];
           
            if(scrol_KBC.contentOffset.x < Auto_Scroll_Var)
            {
                    NSLog(@"Hurray");
                    currentViewLocation = scrol_KBC.contentOffset.x;
                   
                    scrol_KBC.scrollEnabled = NO;
            }      
            [scrol_KBC setDirectionalLockEnabled:YES];
           
    }


    /*
     // Override to allow orientations other than the default portrait orientation.
     - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
     // Return YES for supported orientations
     return (interfaceOrientation == UIInterfaceOrientationPortrait);
     }
     */


    - (void)didReceiveMemoryWarning {
            // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
           
            // Release any cached data, images, etc that aren't in use.
    }

    - (void)viewDidUnload {
            // Release any retained subviews of the main view.
            // e.g. self.myOutlet = nil;
    }


    - (void)dealloc {
            [AVsound release];
        [super dealloc];
    }


    @end