[objective-c]复制到另一个时从一个 NSArray 生成随机图像从第一个数组

发布时间: 2016/10/2 10:48:28
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我正在开发一个应用程序中从 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];

解决方法 1:

创建另一个 ArrayinitViewDidLoad 并添加他们一个接一个,因为生成的对象。

NSString *imageName=[FrontsCards objectAtIndex:randIdx];

[yourNewArray addObject:imageName];
赞助商