【问题标题】:How to Load Different Images On Table View Cell如何在表格视图单元格上加载不同的图像
【发布时间】:2015-06-25 05:32:25
【问题描述】:

我在第一个视图中有两个视图,说 A 用于在单击保存按钮时将图像保存在文档目录中,然后第二个视图说 B。我有两个使用表格的特定单元格上传该图像。在下面的代码中,我正在尝试将图像与特定用户电子邮件 ID 进行比较。电子邮件在两种视图中都很常见。所以我可以用他们的电子邮件区分特定用户吗?在他们的表格视图单元格上,我们以不同的方式显示他们的图像。但我无法做到这一点......如何在 Ios 中实现这一点? AnyOne 可以帮我解决这个问题....我在文档目录中保存和加载图像的代码是 enter code here

查看 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);
    }
}

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

【问题讨论】:

  • 您想在视图 B 中显示您已经下载到文档目录中的相同图像吗?
  • 是的....我想显示我在按钮单击时保存的相同图像..@None
  • 我认为如果您正在获取响应,那么响应包含唯一的用户 ID 和电子邮件?
  • 您是否检查过您是否成功将图像写入文档目录?
  • @None...,显示我加载的图像.. 但在所有表格视图单元格中它们显示相同的图像,我想要特定用户的不同图像

标签: ios objective-c


【解决方案1】:

您可以按以下方式修改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;
}

编辑:你可以得到如下图。

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

希望对您有所帮助。

【讨论】:

  • @ None,...我想在表格视图单元格上为不同的用户显示不同的 iage,Ur 代码不这样做
  • 它与您之前调用的相同,只需传递一个参数,即您的电子邮件..
  • 你可以调用像 [self saveImage:yourImageObject filePath:email];
  • 从 saveImage 方法中移除了 NSLog。
  • 路径变量不存在。
【解决方案2】:

如果您在文档目录中保存了不同的图像,但在加载到 tableview 单元格时,它只显示一个图像,那么只需将您的 imageview 的图像设置为 Nil,然后再设置新图像,如下所示:

cell.imageView.image = Nil;

【讨论】:

    【解决方案3】:

    通过使用下面的代码,我们可以在表格视图单元格上异步粘贴图像。

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

    【讨论】:

      【解决方案4】:

      你也可以这样使用,

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

      并像这样在标头中定义,

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-02
        • 1970-01-01
        • 1970-01-01
        • 2021-07-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多