2

I've a tableview which has list of images and image thumbnail (image list and thumbnails are parsed from JSON object), I'm adding image data objects to imagesArray like this -

ImageData *imageDataObject = [[ImageData alloc]initWithImageId:[[imageListArray    
                         objectAtIndex:indexPath.row] imageId] imageData:imageData];
[imagesArray addObject:imageDataObject];

ImageData object

@property (nonatomic, strong) NSString* imageId;
@property (nonatomic, strong) NSData* imageData;

allImagesArray like this

[ImageData object1,ImageData object2,....]

I want to assign imageData of the object from this array based on selectedImageId to

UIImage* image =[[UIImage alloc] initWithData:........];

I'm not able to think of a way to get to that imageData based on selectedImageId

Please help.

Update - Thank you all for the help, I could do it.

1
  • You should use a single dictionary and add all your images to it: {imageid:imagedata,imageid2:imagedata2,...} Commented Nov 12, 2013 at 3:57

3 Answers 3

1

One of the possible way will be, iterate through the array, find your selectedImageId from the dictionary and use it.

Example:

    ImageData *imageDataObject = nil;

    for(int i=0; i<allImagesArray.count;i++){
        NSDictionary *dict= allImagesArray[i];
        imageDataObject = [dict objectForKey:selectedImageId];
        if(imageDataObject != nil){
           UIImage* image =[[UIImage alloc] initWithData:........];
           //do whatever
           break;
        }
    }

As per your EDIT:

What you have is an array of ImageData objects [ImageData1,ImageData2,...]. For each ImageData object, you have imageId and imageData property and what you want is simply compare the selectedImageId with this imageId and get the imageData from that.

So for that, in your PPImageViewController, you can iterate the allImagesArray like this and get the imageData.

    for(ImageData* imgDataObj in self.allImagesArray){
        if([imgDataObj.imageId isEqualToString:self.selectedImageId]){
           UIImage* image =[[UIImage alloc] initWithData:imgDataObj.imageData];
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

I'm extremely sorry for that misleading the array has ImageData objects, updated the question
1

So you have:

NSArray* allImagesArray = @[@{@"some_image_id_in_NSString_1":@"the data in NSData 1"}, @{@"some_image_id_in_NSString_2":@"the data in NSData 2"}];

As a property of PPImageViewController.

Assuming the imageid is an NSString and imagedata is NSData, you can create a method something like this on PPImageViewController:

- (UIImage*) findSelectedImage
{
    UIImage* selectedImage;

    for(NSDictionary* d in allImagesArray)
    {
        NSString* currentKey = [[d allKeys] objectAtIndex:0];

        if([currentKey isEqualToString:[self selectedImageId]])
        {
            NSData* imageData = [d objectForKey:currentKey];

            selectedImage = [UIImage imageWithData:imageData];

            break;
        }
    }

    return selectedImage;
}

Then call it like this, maybe on your viewDidLoad method:

UIImage* selectedImage = [self findSelectedImage];

Hope it help.

3 Comments

this is throwing error -[ImageData allKeys]: unrecognized selector sent to instance 0x10901cc80 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ImageData allKeys]: unrecognized selector sent to instance 0x10901cc80'
Where is in my code [ImageData allKeys]? Do you mean [d allKeys]? If it is true, so allImagesArray probably does not contain any NSDictionary object.
I'm extremely sorry for that misleading the array has ImageData objects, updated the question
1

I see you are adding ImageData objects directly into the Array. You could have just used a NSDictionary instead. The key can be imageID (assuming it to be unique) and value will be the imageData object. Then pass the dictionary instead of array to PPImageViewController.

NSMutableDictionary *imageData = [NSMutableDictionary dictionary];

ImageData *imageDataObject = [[ImageData alloc]initWithImageId:[[imageListArray    
                         objectAtIndex:indexPath.row] imageId] imageData:imageData];

[imageData setObject:imageDataObject forKey:imageId];

And then within PPImageViewController, you can easily get the imageDataObject based on selected imageID like this:

ImageData *imageDataObject = allImagesDictionary[selectedImageID];

EDIT:

NSArray *imageIndexes = [allImagesDictionary allKeys];

// Now use imageIndexes to populate your table. This will guarantee the order 

// Fetch the imageId
selectedImageID = imageIndexes[indexPath.row];

// Fetch the imageData
ImageData *imageDataObject = allImagesDictionary[selectedImageID];

2 Comments

Yes, I did the same thing but further I've to implement swipe gesture so used NSArray because user can select any image from list so selectedImageId can be anything so knowing indexes of images is hard
You could get key array by [imageData allKeys] and use it to make sure the indexes are proper. This will make sure that objects are picked from the right place and when swipe gesture occurs, the index returned can directly used to first fetch the object from [imageData allKeys] ans that index can be used to fetch imageData.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.