0

我正在开发一个从 NSMutableArray 加载图像的应用程序。它生成随机图像并将其加载到 iOS 中的 UIImageview。我想在加载后将该数组保存到另一个数组中,我该怎么做。提前致谢。

我的代码如下。

//FrontsCards 是我的数组

    for(m=0; m<[FrontsCards count];m++)
    {
        ImgView.alpha=1;

        ImgView.tag=m;

        NSLog(@"Img View Tag %d",ImgView.tag);

        randIdx=arc4random()%[FrontsCards count];

        NSString *imageName=[FrontsCards objectAtIndex:randIdx];

        NSString *fullImageName=[NSString stringWithFormat:@"%@",imageName];

        int padding=0;

        CGRect imageViewFrame=CGRectMake(scrollView.frame.size.width*m+padding, scrollView.frame.origin.y, scrollView.frame.size.width-2*padding, scrollView.frame.size.height);

        ImgView=[[UIImageView alloc]initWithFrame:imageViewFrame];

        [ImgView setImage:[UIImage imageNamed:fullImageName]];

        [scrollView addSubview:ImgView];

        [ImgView setAccessibilityIdentifier:[FrontsCards objectAtIndex:randIdx]];


        UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapImgView:)];
        doubleTap.numberOfTapsRequired = 2;
        doubleTap.delegate = self;



        [self.ImgView setImage:[UIImage imageNamed:fullImageName]];


        NSLog(@"full image name %@",fullImageName);

        NSLog(@"Image Name = %@", fullImageName);

        [self.ImgView setTag:randIdx];


        [self.ImgView addGestureRecognizer:doubleTap];

        self.ImgView.userInteractionEnabled=YES;

    }
    CGSize scrollViewSize=CGSizeMake(scrollView.frame.size.width*[FrontsCards count], scrollView.frame.size.height);
    [scrollView setContentSize:scrollViewSize];
    [self.view addSubview:scrollView];
4

2 回答 2

0

创建另一个ArrayinitViewDidLoad并在生成时一个一个添加对象。

NSString *imageName=[FrontsCards objectAtIndex:randIdx];

[yourNewArray addObject:imageName];
于 2013-05-21T13:06:58.130 回答
0

试试这个片段来复制一个NSMutableArray

NSArray *newArray = [[oldArray mutableCopy]

或者:

NSMutableArray *newArray = [[[NSMutableArray alloc] initWithArray:oldArray copyItems:YES] autorelease];

就在这段代码之后: randIdx=arc4random()%[FrontsCards count];

将索引保存到另一个数组中并用它来指向原始数组。

于 2013-05-21T12:55:58.263 回答