Reputation: 3166
I am displaying multiple images in a UITableViewCell like an thumbnail images. But i am struggling to retrieve the image of selected UITableViewCell. I have to do this for display the image in another ViewController. I am really struggling to fix this issue.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MyCell"];
if (cell==nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
}
jsonDict = [jsonArr objectAtIndex:indexPath.row];
//NSLog(@"imagess%@",jsonDict);
cell.textLabel.text = [jsonDict objectForKey:@"ImageName"];
stringg=[NSString stringWithFormat:@"%@",cell.textLabel.text];
cell.textLabel.textAlignment=NSTextAlignmentCenter;
NSData *imgData = [NSData dataWithBase64EncodedString:[jsonDict objectForKey:@"ImageData"]];
imageView.image=[UIImage imageWithData:imgData];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%@",cell);
NSString *textname=[NSString stringWithFormat:@"%@",cell.textLabel.text];
NSLog(@"%@",textname);
UIImage *image1 =selectedCell.imageView.image;
age.ageimgae=image1;
}
Upvotes: 0
Views: 213
Reputation: 82769
In didSelectRowAtIndexPath
u need to identify the particular Indexpath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
jsonDict = [jsonArr objectAtIndex:indexPath.row];
NSLog(@"the textr Name==%@",[jsonDict objectForKey:@"ImageName"]);
UIImage *image1 = [UIImage imageWithData:[NSData dataWithBase64EncodedString:[jsonDict objectForKey:@"ImageData"]]];
NSData *imageData = UIImagePNGRepresentation(image1);
[[NSUserDefaults standardUserDefaults] setObject:imageData forKey:@"imageDataKey"];
[[NSUserDefaults standardUserDefaults]synchronize];
NSLog (@"Data Saved");
}
here your choice u pass the NSData or your image to another view controller using NSUSerdefault
or singleton
or SharedInstance
, something else..
in secondviewcontroler.m
nsdata *imagedata=[[NSUserDefaults standardUserDefaults] objectForKey@"imageDataKey"];
yourimgaename.image = [UIImage imageWithData:[NSData dataWithBase64EncodedString: imagedata]];
Upvotes: 0
Reputation: 630
Instead of fetching the image of the selected cell, you can use the index of the selected cell in order to fetch the required image in your JSonArray
(just like you did in in your tableView cellForRowAtIndexPath:
):
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
jsonDict = [jsonArr objectAtIndex:indexPath.row];
NSData *imgData = [NSData dataWithBase64EncodedString:[jsonDict objectForKey:@"ImageData"]];
UIImage *image1 = [UIImage imageWithData:imgData];
}
Upvotes: 1