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

我在第一个视图中有两个视图说A用于保存文档目录中的图像保存按钮单击然后第二个视图说B.我有两个上传该图像与表的特定单元格.在下面的代码我试图比较图像具有特定用户电子邮件id.Email在两个视图中都很常见.因此,我可以将特定用户与他们的电子邮件区分开来,并且在他们的表格视图单元格中,我们以不同的方但我无法做到这一点……如何在 Ios中实现这一目标? AnyOne可以帮助我解决这个问题….我的代码用于保存和加载文档目录中的图像
在此处输入代码

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

解决方法

您可以按照以下修改saveImage和loadImage函数.

-(void)saveImage: (UIImage*)image filePath:(Nsstring*)fileNameEmail
{
     if (image != nil)
     {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,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,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;
}

这可能对你有所帮助.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...