Ios 使用Grand Central Dispatch将URL字符串从JSON转换为ImageView的图像
在其他数据(特别是字符串)中,我从JSON中提取了一个URL,并保存在数组“jsonArray”中。我需要将URL(用于依赖于用户登录的图像)转换为实际图像,以便在imageview“imageProfPic”中显示。我对GCD不太熟悉,因此我非常感谢您对我的代码和在imageProfPic中成功显示我的图像所给予的所有帮助 (编辑:忘记提到我收到了错误“\u NSCFString isFileURL”) TableViewController.m文件Ios 使用Grand Central Dispatch将URL字符串从JSON转换为ImageView的图像,ios,objective-c,json,imageview,grand-central-dispatch,Ios,Objective C,Json,Imageview,Grand Central Dispatch,在其他数据(特别是字符串)中,我从JSON中提取了一个URL,并保存在数组“jsonArray”中。我需要将URL(用于依赖于用户登录的图像)转换为实际图像,以便在imageview“imageProfPic”中显示。我对GCD不太熟悉,因此我非常感谢您对我的代码和在imageProfPic中成功显示我的图像所给予的所有帮助 (编辑:忘记提到我收到了错误“\u NSCFString isFileURL”) TableViewController.m文件 NSURL *myURL = [[
NSURL *myURL = [[NSURL alloc]initWithString:@"http://domain.com/json2.php"];
NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];
NSError *error;
jsonArray = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:&error];
[tableView reloadData]; // if tableView is unidentified make the tableView IBOutlet
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return jsonArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NeedCardTableViewCell *cell = (NeedCardTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"needCard" forIndexPath:indexPath];
NSDictionary *needs = jsonArray[indexPath.row]; // get the data dict for the row
cell.textNeedTitle.text = [needs objectForKey: @"needTitle"];
cell.textNeedPoster.text = [needs objectForKey: @"needPoster"];
cell.textNeedDescrip.text = [needs objectForKey: @"needDescrip"];
dispatch_async(dispatch_queue_create("imageQueue", NULL), ^{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:needs[@"userImage"]]];
dispatch_async(dispatch_get_main_queue(), ^{
[cell.imageProfPic setImage:image];
});
});
return cell;
@interface NeedCardTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *textNeedTitle;
@property (weak, nonatomic) IBOutlet UILabel *textNeedPoster;
@property (weak, nonatomic) IBOutlet UILabel *textNeedDescrip;
@property (weak, nonatomic) IBOutlet UIImageView *imageProfPic;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
TableViewController.h文件
NSURL *myURL = [[NSURL alloc]initWithString:@"http://domain.com/json2.php"];
NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];
NSError *error;
jsonArray = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:&error];
[tableView reloadData]; // if tableView is unidentified make the tableView IBOutlet
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return jsonArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NeedCardTableViewCell *cell = (NeedCardTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"needCard" forIndexPath:indexPath];
NSDictionary *needs = jsonArray[indexPath.row]; // get the data dict for the row
cell.textNeedTitle.text = [needs objectForKey: @"needTitle"];
cell.textNeedPoster.text = [needs objectForKey: @"needPoster"];
cell.textNeedDescrip.text = [needs objectForKey: @"needDescrip"];
dispatch_async(dispatch_queue_create("imageQueue", NULL), ^{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:needs[@"userImage"]]];
dispatch_async(dispatch_get_main_queue(), ^{
[cell.imageProfPic setImage:image];
});
});
return cell;
@interface NeedCardTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *textNeedTitle;
@property (weak, nonatomic) IBOutlet UILabel *textNeedPoster;
@property (weak, nonatomic) IBOutlet UILabel *textNeedDescrip;
@property (weak, nonatomic) IBOutlet UIImageView *imageProfPic;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
如果您的意思是显示来自JSON的图像,那么我使用了JSON,并且它很容易与我一起工作 将UIImageView+网络缓存类别与UITableView一起使用 只需#导入UIImageView+WebCache.h头,并从tableView:cellForRowAtIndexPath:UITableViewDataSource方法调用setImageWithURL:Placeholder图像:方法。一切都将为您处理,从异步下载到缓存管理
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
cell.textLabel.text = @"My Text";
return cell;
}
使用块
使用块,您可以收到有关映像下载进度的通知,以及每当映像检索成功或未成功完成时:
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {... completion code here ...}];
我认为你只需要用你的字符串创建一个NSURL,你应该很好。试一试:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSURL *imageURL = [NSURL URLWithString:needs[@"userImage"]];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
dispatch_async(dispatch_get_main_queue(), ^{
[cell.imageProfPic setImage:image];
});
});
我在早些时候访问SDWebImage GitHub站点时也尝试过这个代码片段。谢谢,但我只是对库不太熟悉,我也不确定具体是谁在我的代码/情况中实现了这一点。下载库,并将其粘贴到项目中确保选中“将项目复制到目标组”然后在TableViewController.m文件中导入UIImageView+WebCache.h,就可以开始了。您是否检查了试图从中获取图像的url以确保其具有有效的图像?@Inertiatic是的,它是有效的。请查看。这可能对你有帮助。非常感谢!这需要3个问题才能解决!我非常感激!令人惊叹的!很高兴它对您有用,因为NSString是一个“url”@dvdsown,所以这个小细节很容易被忽略。不幸的是,这只修复了一个错误。另一个错误仍然存在(由于一个单元格被重用,并且在执行完成处理程序时被重新分配了一个不同的行)因为这个错误是第二大问题,所以我相信OP会从15000个答案中的一个找到解决方案,不过:)@CouchDeveloper自从我发布了这个答案后,我已经摆脱了所有的GCD,开始使用AFD网络。一旦我把它安装好,它就像一个符咒。谢谢你的帮助!