2017-09-27 77 views
0

我是这方面的新人。我想为孩子们的游戏创建一个应用程序。 我已经使用这段代码。如何清除存储在UIImage中的图像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从代码,但在此之前,我需要清除的图像如何做到这一点。

+0

你可以把它具体些吗?你想清除屏幕上的图像('UIImageView')还是从内存(意味着变量 - '图像'mutableArray)?或者您已经使用我的[解决方案](https://stackoverflow.com/a/46439982/4061501) –

+0

修复了您的问题。实际上,您无需为相同问题添加更多信息而创建新问题。你可以[编辑](https://stackoverflow.com/posts/46439689/edit)它。 –

回答

0

对于删除整个阵列,请使用以下行: -

[yourArray removeAllObjects]; 

和去除的UIImageView图像,请使用以下行: -

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

我已编辑我的代码如何在该代码中实现。 – teena

+1

你已经创建了图像作为NSMutableArray的权利,你想删除所有我猜想的图像,所以请使用我的第一个建议。 只要你想清除你的数组只需使用这个: [images removeAllObjects]; –

+0

是否有效? @teena –

0

您可以删除来自NSMutableArray的元素通过调用​​。 在你的情况下,从images阵列被删除UIImages

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

    } 
    ... 
    ... 
} 
+0

谢谢你的代码作品..... – teena

+0

请接受它 –