简体   繁体   English

如何在表视图单元格上加载不同的图像

[英]How to Load Different Images On Table View Cell

I have two views in 1st view say A for save an image in document directory on save button click and then second view say B. I have two upload that image with specific cell of the table.In below code i am trying to compare the image with specific user email id.Email is common in both views. 我在第一个视图中有两个视图说A用于保存文档目录中的图像保存按钮单击然后第二个视图说B.我有两个上传该图像与表的特定单元格。在下面的代码我试图比较图像具有特定用户电子邮件id.Email在两个视图中都很常见。 So can i differentiate the specific user with their email and on their table view cell we show their images differently. 因此,我可以将特定用户与他们的电子邮件区分开来,并且在他们的表格视图单元格中,我们以不同的方 But i am not able to do that... How can this to be achieved in Ios? 但我无法做到这一点......如何在Ios中实现这一目标? AnyOne can help me out for this....My Code for save and load image in document directory are enter code here AnyOne可以帮我解决这个....我在文件目录中保存和加载图像的enter code here

View A 查看A.

-(void)saveImage: (UIImage*)image
{
    if (image != nil)
    {
        NSArray *paths =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString* path = [documentsDirectory stringByAppendingPathComponent:
                              @"iosImage.png" ];
        NSString *fileName = [path stringByAppendingFormat:email];
        NSLog(@"The result of Concatenate String =%@",fileName);
        NSData* data = UIImagePNGRepresentation(image);
                  [data writeToFile:path atomically:YES];
        NSLog(@"The Path of the directory %@",path);
    }
}

View B 查看B.

- (UIImage*)loadImage
{
    NSLog(@"Email^^^^^^^^^%@",imgEmail);
    NSError *error;
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSArray *paths =        NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:
                          @"iosImage.png" ];
    UIImage* image = [UIImage imageWithContentsOfFile:path];
    NSLog(@"Documents directory: %@",[fileMgr     contentsOfDirectoryAtPath:documentsDirectory error:&error]);
    return image;
}

You can modified saveImage and loadImage function as per below. 您可以按照以下修改saveImageloadImage函数。

-(void)saveImage: (UIImage*)image filePath:(NSString*)fileNameEmail
{
     if (image != nil)
     {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileNameEmail];

        NSData* data = UIImagePNGRepresentation(image);
              [data writeToFile:filePath atomically:YES];

     }
}

- (UIImage*)loadImage:(NSString*)fileNameEmail
{
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileNameEmail];
     UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath];
     return image;
}

Edit : You can get specific image like below. 编辑 :您可以获得如下特定图像。

-(UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    cell.youimageview.image = [self loadImage:[[yourArray indexOfObject:indexPath.row] valueForKey:@"youemailkey"]];
    return cell;
}

may this help you. 这可能对你有所帮助。

如果您在文档目录中保存了不同的图像,但是在tableview单元格中加载时,它会显示一个图像,然后在设置新图像之前将图像视图的图像设置为Nil,如下所示:

cell.imageView.image = Nil;

By using the below code we can asynchronously paste image on table view cell. 通过使用下面的代码,我们可以在表视图单元格上异步粘贴图像。

NSString *imageName=[NSString stringWithFormat:@"%@.png",d.emailId];

UIImage *image = [self loadImage:imageName];  
if (image==nil)
{
    [imgProfilePic setImage:[UIImage imageNamed:@"userEdit.png"]];
}
else
{
    [imgProfilePic setImage:image];
}
photos =image;

NSLog(@"^^^^^^^^^^^^^^%@",photos);

[cell.contentView addSubview:imgProfilePic];

Also you can use like that, 你也可以这样使用,

-(NSString *)ImageString{

    NSData *imgdata = UIImageJPEGRepresentation(self.imgView.image, 0.5);
    NSString *imagename =[self generateImageName];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager createFileAtPath:[NSString stringWithFormat:@"%@/%@",   DOCUMENTS_FOLDER,imagename]
                         contents:imgdata
                       attributes:nil];
    return imagename;

    }
       -(NSString *)generateImageName{

        int timestamp = [[NSDate date] timeIntervalSince1970];
        NSString *StrImage = [NSString stringWithFormat:@"Image_%d.png",timestamp];
        NSLog(@"image name=%@",StrImage);
        return StrImage;
    }

And Define In Header like that, 并像这样定义在标题中,

#define DOCUMENTS_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ios表视图单元格按行索引顺序加载图像 - ios table view cell load images in the order of row index 在iOS中一起加载所有表格视图单元格图像 - Load all table view cell images all together in iOS 如何将不同的图像解析到表视图可重用单元格中? - How can I parse different images into my table view reusable cell? 如何在表格视图单元格内快速加载两个不同的布局 - How do I load two different layouts inside a table view cell in swift Table View加载更多单元格? - Table View load more cell? 迅速-如何在自定义视图单元格中显示两个不同的图像 - swift - how to display two different images in a custom made view cell 如何为不同的UITableView节使用不同的表视图单元格类 - How to use different table view cell class for different UITableView sections 如何在 TableViewCell.swift 文件中配置包含图像的按钮,并实现在每个表格视图单元格中显示不同的图像? - How to configure a button containing an image in TableViewCell.swift file, and implement showing different images in each table view cell? 如何将多个图像添加到Swift iOS中的自定义表格视图单元格? - How to add multiple images to to custom table view cell in swift ios? 在选择表格单元格“ didSelect”时,如何将图像解析到另一个视图? - On selecting the table cell “didSelect” how to parse images to another View?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM