0

Hi Iam working with Iphone Photo Library. I got all photos from photo library with AssetFramework. I displayed those photos in scrollview and are perfectly displaying and images count is assume 6. Then when iam clicking single image, it will show large image. it is also done. my problem is "count is 12 (double count) when clicking image to show it as large."

I used below code to get images:

 - (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);
}

}

Please notice that i created button and action is userImageClicked in above method. when iam clicking userImageClicked button, the array count is double.

i dont know why this is happened. i try to remove duplicates using containsObject method. but no use.

In above method, I saved UIImage in objectclass and assinging that object to imagesArray.

I also took NSMutableSet to store value, but it is also no use.

Please any one can suggest to solve my issue.

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
Mahesh Peri
  • 1,689
  • 2
  • 14
  • 24

4 Answers4

1

This is how you delete duplicate data:

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

Please use it accordingly...

A simpler way:

NSMutableArray *unique = [NSMutableArray array];

for (id obj in originalArray) {
    if (![unique containsObject:obj]) {
        [unique addObject:obj];
    }
}
lakshmen
  • 28,346
  • 66
  • 178
  • 276
  • And this answer is refer from:http://stackoverflow.com/questions/1025674/the-best-way-to-remove-duplicate-values-from-nsmutablearray-in-objective-c & http://stackoverflow.com/questions/1858649/how-to-remove-duplicate-value-in-nsmutablearray... – Vishal Apr 19 '13 at 06:36
  • i tried this, but not working – Mahesh Peri Apr 19 '13 at 06:37
  • tried this, simpler... – lakshmen Apr 19 '13 at 06:39
  • Thanks for your reply, but i tried these all what i found in stackoverflow. above code you provide is not working because i have UIImage in object. when i logging, the output is uniqueSet/userImgClck is {(    ,    ,    ,    ,    ,    ,    ,    ,    ,    ,    ,     )} and count is 12. all are different image names, but 6 images are repeated. – Mahesh Peri Apr 19 '13 at 06:50
  • same for nsset or nsmutablearray – Mahesh Peri Apr 19 '13 at 06:50
  • from you Log the situation is that you are creating the objects and all of them are deferent objects, that b/c when you check `[mutableArray indexOfObject:object]` it returns that there is no same object and continue to add object to muTable – Nazir May 16 '13 at 14:32
  • Control where you create the objects UIImage – Nazir May 16 '13 at 14:33
0

If above is the only method where you add images to imagesArray then try checking you are not calling it twice from any where, else check if imagesArray is not getting updated from any where else.

And if you need to call this method createScrollView more than once then write code for adding image in imagesArray separately.

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
0

The problem is in the creation of galleryObj. You create a new object for every image. Every instance of galleryObj is therefore unique.

Whether you use NSSet of NSMutableArray or NSWhatever, they will not know that inside the galleryObj object is the same UIImage.

The Apple documentation of ALAssetRepresentation states that it encapsulates one of the representations. So if you have both a JGP and a RAW picture saved, you will get two separate representations of the same picture.

In that case you have to ignore one or the other type. I would not recommend comparing a RAW picture with a JPG picture (;-)).

uulhaa
  • 121
  • 3
0

Hi Thank all for quick reply. after 3 hours i solved my problem. I got exact duplicates, so that i removed duplicates using below code. i know it is not good code. but it solves my issue.

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

}

and also i find why the duplicate data is? the answer is i used the block code, it is calling two times. so that array contains double data values.

Thanks you all..

Mahesh Peri
  • 1,689
  • 2
  • 14
  • 24