如何清除存储在UIImage objective-c

时间:2017-09-27 04:46:52

标签: ios objective-c

我是这个领域的新手。我想为儿童游戏创建一个应用程序。 我使用过这段代码。

我已将图像存储在数组中,我需要清除存储图像。那么如何清除所有数组值。

我的代码:

    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];
}

在b == d等于然后我从代码调用函数selectnext但在此之前我需要清除图像如何做到这一点。

2 个答案:

答案 0 :(得分:0)

要删除整个数组,请使用以下行: -

[yourArray removeAllObjects];

要从UIImageView中删除图片,请使用以下行: -

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

答案 1 :(得分:0)

您可以通过调用NSMutableArrayremoveAllObjects中删除元素。 在您的情况下,请从UIImages数组中删除images

-(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];

    }
    ...
    ...
}