我在第一个视图中有两个视图说A用于在保存按钮单击时保存文档目录中的图像然后第二个视图说B.我有两个上传该图像与表的特定单元格。在下面的代码中我试图将图像与特定用户电子邮件ID进行比较.Email在两个视图中都很常见。因此,我可以将特定用户与他们的电子邮件区分开来,并且在他们的表格视图单元格中,我们以不同的方但我无法做到这一点......如何在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;
}
答案 0 :(得分:2)
您可以按照以下修改saveImage
和loadImage
功能。
-(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;
}
这可能对你有所帮助。
答案 1 :(得分:1)
如果您已在文档目录中保存了不同的图像,但在tableview单元格中加载时,它会显示一个图像,然后在设置新图像之前将图像视图的图像设置为Nil:
cell.imageView.image = Nil;
答案 2 :(得分:0)
通过使用以下代码,我们可以在表格视图单元格上异步粘贴图像。
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];
答案 3 :(得分:0)
你也可以这样使用,
-(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"]