[英]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. 您可以按照以下修改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;
}
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.