简体   繁体   中英

how to clear the image stored in UIImage objective-c

I am new in this field .I want to creating an app for kids gaming. I have used this code.

I have stored the image in array and i need to clear the store image .So how to clear all array value.

my code :

    images=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"Navarre-Family-Eye-Care-Navarre-Florida-Optometrist-Santa-Christmas-Toy-Safety.jpg"],[UIImage imageNamed:@"Christmas-Wallpapers-HD-Picture.jpg"],[UIImage imageNamed:@"Christmas-Wallpaper-jesus-9413550-1024-768.jpg"],[UIImage imageNamed:@"tree.jpg"],[UIImage imageNamed:@"luxury-christmas-napkins-father-christmas-1635-p.jpg"],[UIImage imageNamed:@"Navarre-Family-Eye-Care-Navarre-Florida-Optometrist-Santa-Christmas-Toy-Safety.jpg"],[UIImage imageNamed:@"Christmas-Wallpapers-HD-Picture.jpg"],[UIImage imageNamed:@"Christmas-Wallpaper-jesus-9413550-1024-768.jpg"],[UIImage imageNamed:@"tree.jpg"],[UIImage imageNamed:@"luxury-christmas-napkins-father-christmas-1635-p.jpg"], nil];

    NSString *dd=[NSString stringWithFormat:@"%@", images];
    NSLog(@"%@",dd);




    //used random number for image...and got a random image

    randomIndex1=arc4random() % images.count;
    selectedImage = [images objectAtIndex:randomIndex1]; //random selected image

    NSLog(@"%@",selectedImage);





    //stored 1 to 10 numbers in array...
    number = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil];

    //used random numbers in numbers...and got a random number....
    randomNumber = arc4random() % number.count; //random selected number

    num= [number objectAtIndex:randomNumber];

    ss=[NSString stringWithFormat:@"%@",num];

    NSLog(@"%@",ss);

    b = [ss integerValue];

    //NSLog(@"%ld",(long)b);





    //dispalying the image based on random number
    //  for (int i = 0; i <b; i++)
    // {
    // NSLog(@"%@", selectedImage);

    // _img.image = selectedImage;

    //cell.img.image=selectedImage;

    //}


    //used tag on UIImageView to display the image....

    for (int j = 1 ; j <=b ; j++){
        id subView = [self.view viewWithTag:j];

        if ([subView isKindOfClass:[UIImageView class]]){
            ((UIImageView *)subView).image = selectedImage;

        }
    }

}

-(void)check{

    if (d==b) {



        [self selectnext];

    }
    else
    {
        UIAlertController * alert=[UIAlertController alertControllerWithTitle:@"Title"
                                                                      message:@"Message"
                                                               preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction* yesButton = [UIAlertAction actionWithTitle:@"Yes, please"
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action)
        {
            /** What we write here???????? **/
            NSLog(@"you pressed Yes, please button");

            // call method whatever u need
        }];

        UIAlertAction* noButton = [UIAlertAction actionWithTitle:@"No, thanks"
                                                           style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * action)
        {
            /** What we write here???????? **/
            NSLog(@"you pressed No, thanks button");
            // call method whatever u need
        }];

        [alert addAction:yesButton];
        [alert addAction:noButton];

        [self presentViewController:alert animated:YES completion:nil];    }
}

-(IBAction)button11:(id)sender
{
    d=1;
    [self check];
}
-(IBAction)button12:(id)sender
{
    d=2;
    [self check];
}

at b==d is equal then i call the function selectnext from code but before that i need to clear the image how to do that.

For removing whole array , please use below line :-

[yourArray removeAllObjects];

And For removing the Image from UIImageView , please use below line :-

UIImageView *imgV = [[UIImageView alloc] initWithImage:nil];

You can remove elements from NSMutableArray by calling removeAllObjects . In your case, remove UIImages from images array by,

-(void)check{

      if (d==b) {
        [images removeAllObjects];
        for (int j = 1 ; j <=b ; j++){
           id subView = [self.view viewWithTag:j];

           if ([subView isKindOfClass:[UIImageView class]]){
            ((UIImageView *)subView).image = nil;

         }
      }
      [self selectnext];

    }
    ...
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM