【问题标题】:How to remove duplicate data from nsmutableArray [duplicate]如何从 nsmutableArray 中删除重复数据 [重复]
【发布时间】:2013-04-12 11:43:33
【问题描述】:

您好,我正在使用 Iphone 照片库。我使用 AssetFramework 从照片库中获取了所有照片。我在滚动视图中显示了这些照片并且完美显示并且图像计数假设为6。然后当我单击单个图像时,它将显示大图像。它也完成了。我的问题是“单击图像以将其显示为大时计数为 12(双倍计数)。”

我使用下面的代码来获取图像:

 - (void)createScrollView
 {

@try
{
    NSLog(@"in create scrollview");

    //add views to scrolview
   // UIImageView *backgroundImgView;
    int x=5;
    int y=7;
    NSLog(@"assetsArray/count/createScrollview %d",assetsArray.count);
    for (int i=0;i<[assetsArray count];i++)
    {
        UIView *userView=[[UIView alloc] initWithFrame:CGRectMake(x, y, 70, 80)];
        userView.tag=i;
        UIImageView *backgroundImgView=[[UIImageView alloc] initWithFrame:CGRectMake(1, 1, 70, 70)];

        backgroundImgView.tag=1;

        // [backgroundImgView setImageWithURL:[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF16BigEndianStringEncoding]] placeholderImage:[UIImage imageNamed:@"NoImage.png"]];

        //-------------Getting Images from AssetsLibrary ----------
        ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
        {
            galleryObj=[[GalleryObject alloc]init];
            ALAssetRepresentation *rep = [myasset defaultRepresentation];
            CGImageRef iref = [rep fullResolutionImage];
            UIImage *assetsLibraryImage;
            if (iref)
            {
                assetsLibraryImage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];
                galleryObj.galleryImage=assetsLibraryImage;

            }
            else
            {
                assetsLibraryImage = [UIImage imageNamed:@"NoImage.png"];
            }
            //[set addObject:[NSString stringWithFormat:@"1"]];
            [uniqueSet addObject:galleryObj];
            NSLog(@"uniqueSet data is .....%@",uniqueSet); // Output (3,1,4,2,5) ... all objects

            [imagesArray addObject:galleryObj];
            NSLog(@"imagesArray/resultBlock count is %d array is %@....",imagesArray.count,imagesArray);

            backgroundImgView.image=assetsLibraryImage;
        };


        ALAsset *al_asset = [assetsArray objectAtIndex:i];
        //NSLog(@"al_asset is ......%@",al_asset);
        al_assetUrl=al_asset.defaultRepresentation.url;
        //NSLog(@"al_assetUrl is %@",al_assetUrl);

        ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
        {
            NSLog(@"ALAssetsLibraryAccessFailureBlock");
        };

        ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
        [assetslibrary assetForURL:al_assetUrl resultBlock:resultblock failureBlock:failureblock];


        //-------------Getting Images from AssetsLibrary ----------
        UIButton *userButton=[[UIButton alloc]initWithFrame:CGRectMake(1, 1, 70,70)];
        [userButton addTarget:self action:@selector(userImageClicked:) forControlEvents:UIControlEventTouchUpInside];
        userButton.tag=i;

                 [userView addSubview:backgroundImgView];
        [userView addSubview:userButton];

        [self.galleryScrollview addSubview:userView];

        x+=79;

        if ((i+1)%4==0)
        {
            //if added image is 4th image
            y+=80;
            x=5;
        }
       // [activity stopAnimating]; 

    }

    if (y+100>self.galleryScrollview.frame.size.height)
    {
        self.galleryScrollview.contentSize=CGSizeMake(320, y+100);
    }

    else
    {
        self.galleryScrollview.contentSize=CGSizeMake(320, self.galleryScrollview.frame.size.height+60);
    }
}
@catch (NSException *exception)
{
    NSLog(@"exception is %@",exception);
}

}

请注意,我在上述方法中创建了按钮和操作是 userImageClicked。当我单击 userImageClicked 按钮时,数组计数是双倍的。

我不知道为什么会这样。我尝试使用 containsObject 方法删除重复项。但没用。

在上述方法中,我将UIImage 保存在objectclass 中,并将该对象分配给imagesArray

我也拿了NSMutableSet来储值,但是也没用。

请任何人都可以建议解决我的问题。

【问题讨论】:

  • 使用 AssetUrl 验证每个资产的唯一性
  • 为什么工作方向错误删除重复数据,努力不要添加重复数据
  • 不想给出答案,就好像你只把你的标题放在谷歌前 5-6 参考。将来自 SO。
  • 为什么不用NSSet?

标签: iphone ios objective-c ios5 ios6


【解决方案1】:

您好感谢大家的快速回复。 3小时后,我解决了我的问题。我得到了完全相同的重复项,因此我使用下面的代码删除了重复项。我知道这不是好的代码。但它解决了我的问题。

if([imagesArray count]>0)
{
    int dupCount=[imagesArray count]/2;
    int imagesArrayCount=[imagesArray count];
    for(int i=dupCount;dupCount<imagesArrayCount;dupCount++)
    {
        [imagesArray removeObjectAtIndex:i];
    }

}

我也发现为什么重复数据是?答案是我使用了块代码,它调用了两次。因此该数组包含双数据值。

谢谢大家..

【讨论】:

    【解决方案2】:

    问题在于galleryObj 的创建。 您为每个图像创建一个新对象。 因此,galleryObj 的每个实例都是唯一的。

    无论你使用 NSSet 的 NSMutableArray 还是 NSWhatever,他们都不会知道 galleryObj 对象内部是同一个 UIImage。

    ALAssetRepresentation 的 Apple documentation 声明它封装了 一个 表示。因此,如果您同时保存了 JGP 和 RAW 图片,您将获得同一图片的两个单独表示。

    在这种情况下,您必须忽略其中一种类型。我不建议将 RAW 图片与 JPG 图片进行比较 (;-))。

    【讨论】:

      【解决方案3】:

      如果上面是您将图像添加到imagesArray 的唯一方法,请尝试检查您没有从任何地方调用它两次,否则检查imagesArray 是否没有从其他任何地方更新。

      如果您需要多次调用此方法createScrollView,请分别编写代码以在imagesArray 中添加图像。

      【讨论】:

        【解决方案4】:

        这是删除重复数据的方式:

        NSArray *copy = [mutableArray copy];
        NSInteger index = [copy count] - 1;
        for (id object in [copy reverseObjectEnumerator]) {
            if ([mutableArray indexOfObject:object inRange:NSMakeRange(0, index)] != NSNotFound) {
                [mutableArray removeObjectAtIndex:index];
            }
            index--;
        }
        

        请相应地使用它...

        更简单的方法:

        NSMutableArray *unique = [NSMutableArray array];
        
        for (id obj in originalArray) {
            if (![unique containsObject:obj]) {
                [unique addObject:obj];
            }
        }
        

        【讨论】:

        • 我试过了,但没用
        • 试过这个,更简单...
        • 感谢您的回复,但我尝试了这些我在 stackoverflow 中找到的所有内容。您提供的上述代码不起作用,因为我在对象中有 UIImage。当我登录时,输出是 uniqueSet/userImgClck 是 {( , , , , , , , , , , , )} 和计数是 12。都是不同的图像名称,但 6 个图像重复。
        • nsset 或 nsmutablearray 相同
        猜你喜欢
        • 2013-04-20
        • 2011-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-17
        • 1970-01-01
        • 2019-07-28
        • 1970-01-01
        相关资源
        最近更新 更多